midhun k midhun k - 3 months ago 10
Node.js Question

mongo db insertion not working

I am new to MongoDb, and i have a doubt in insertion of data.
My mongoose schema for 'user' collection:

var user = new mongoose.Schema({

username : {type: String},
email : {type: String,index: {unique: true}},
password : {type: String},
feed : [{
title : {type: String},
description : {type: String},
latitude : {type:Number},
longitude : {type:Number},
feedImages : [{imageUrl: {type: String}}]
}]
});


Here i inserted data to username,email and pasword in my first service call:

app.post('/users',function(req,res) {

var username = req.body.username;
var email = req.body.email;
var password = req.body.password;

var userData = {'username':username,'email':email,'password':password};
new db.user(userData).save(function(err,result){
if (err) {
res.json({"success": '0', "message": "Error adding data"});
}
else {
res.json({"success": '1', "message": "Data added"});
}
});
});


Then i tried to insert data to feed for the above id.

app.post('/feeds',function(req,res) {
var _id = req.body._id;
var title = req.body.title;
var description = req.body.description;
var latitude = req.body.latitude;
var longitude = req.body.longitude;
db.user.update(
{_id:_id },
{$push : {
feeds:[{
title: title,
description: description,
latitude:latitude,
longitude:longitude

}]
}
}
,function (err,result) {
console.log(err);
if (err) {
res.json({"success": '0', "message": "Error adding data"});
}
else {
res.json({"success": '1', "message": "Data added"});
}
});

});


No error is shown, but the data insertion is not happening.please help me to sort out this problem...

Answer

Your latitude and longitude should be converted to numbers:

var latitude    = Number(req.body.latitude);
var longitude   = Number(req.body.longitude);