I am trying to save a document to MongoDb using NodeJS. I am using Express, Mongoose, Node-Restful to do the hard work for me. However, although it does create the document in the MongoDb their is no actual content. I only get
[ { "_id": "5460ab83c9e8f9481063df82", "__v": 0 }, { "_id": "5460ac00c9e8f9481063df83", "__v": 0 }, { "_id": "5460ae36c9e8f9481063df85", "__v": 0 }, { "__v": 0 }, { "_id": "5460afb6be7938bc1469ae97", "__v": 0 }, { "_id": "5460fe9088d68e2516cdf572", "__v": 0 }, { "_id": "5460fecfd47cba3916fba0ab", "__v": 0 }, { "_id": "5460fed5d47cba3916fba0ac", "__v": 0 }, { "_id": "5460fed8d47cba3916fba0ad", "__v": 0 }, { "_id": "5460ff079b60285016dfd0da", "__v": 0 } ]
As you can see the actual content is not being saved which for testing purposes is only a name.
My Schema (models/users.js)
var restful = require('node-restful');
var mongoose = restful.mongoose;
var userSchema = new mongoose.Schema({
name: String
});
module.exports = restful.model('user',userSchema );
My api (api/api.js)
var express = require('express');
var router = express.Router();
var userModel = require('../models/users')
userModel.methods(['get','put','post','delete']);
userModel.register(router,'/user');
module.exports = router;
and the app code (server.js)
console.log("Adding Requires");
var express = require('express');
var fs = require('fs');
var urlEncoded = require('urlencoded-request-parser');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var override = require('method-override');
var rest = require('node-restful');
console.log("Creating Server Object");
var server = express();
server.locals.moment = require('moment');
server.set('ServerVersion', "5.0.1.7");
console.log("Server setting port");
var port = process.env.port || 1337;
console.log("Connecting to MongooseDB");
mongoose.connect("mongodb://mvm-mongodb.cloudapp.net/ccslabs-main");
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('Connected to MongoDB successfully');
});
server.disable('x-powered-by');
console.log("Server setting uses");
server.use(urlEncoded());
server.use(bodyParser.urlencoded({extended: true}));
server.use(bodyParser.json());
server.use(override());
console.log("Server setting routes");
var routePath="./routes/";
fs.readdirSync(routePath).forEach(function(file) {
var route=routePath+file;
console.log("\tAdding routes: " + route)
require(route)(server);
});
console.log("\tAdding API routes");
server.use('/api',require('./api/api'));
console.log("\tAdding Static Files routes");
server.use(express.static(__dirname + "/content"));
console.log("Server creating sets");
server.set('view engine', 'ejs');
server.set('views', __dirname);
server.listen(port);
console.log("Server listening on port " + port);
The output from nodemon on running server.js is
Adding Requires
Creating Server Object
Server setting port
Connecting to MongooseDB
Server setting uses
Server setting routes
Adding routes: ./routes/home.js
Adding routes: ./routes/login.js
Adding routes: ./routes/submission.js
Adding routes: ./routes/testing.js
Adding API routes
Adding Static Files routes
Server creating sets
Server listening on port 1337
Connected to MongoDB successfully
I populate the db using Postman chrome extension an empty document is created without the data I posted in the form of a key value pair. name "dave"
So to summarise I send data to the mongodb via node-restuful and mongoose however, although the document is created it does not contain the data. Can anyone see the problem?
Add a header in postman of
Content-Type : application/json
and instead of using form fields, send it with as raw json. Worked for me.
I never comment on stackoverflow but this was an issue for me several times and I seem to keep running into this. So, I'll like to HELP everyone.
First Set Header to Content-Type : application/json
Don't include :
into Context-type
.
Second choose raw for the input type. Though this isn't a JSON tutorial, postman notice the difference between single and double quotes.
Use double quotes which is this "
, NOT '
and not ''
. Second, double quote the key and if the value of the key is a string, double quote the expression.
It was always recommended that keys should be single strings (use camel hump notation if necessary). Being a so-called veteran of programming,
I though postman would handle if the keys for the JSON object was variables and not string because of the notation but maybe it is an issue with postman. A picture tells a thousand words so I included a picture.
Note that if you have issues add particular key, it might be your model. Go young grasshopper. (A)lways (B)e (C)oding
Regards!
Quentin Mayo.
P.S I have no affiliation with Postman but I strongly recommend it to anyone trying to work with an API.
I don't post enough to post images so if someone validates me, feel free to post re-post the image on this page(I just want to help others). http://postimg.org/image/6xmto3elj/
What worked for me was selecting x-www-form-urlencoded rather than the default selection of form-data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With