javascript - How to push returned value into an array mongodb? -
i getting correct data friendrequests getting user id , throwing in friendrequest field of mongoose file. when add $push add data friendrequest array in route file, not insert , gives me err function created.
here route file:
exports.addcontactpost = function(req, res, err) { user.findbyidandupdate(req.signedcookies.userid, { $push: {friendrequest: req.body.friendrequest} }, function(err) { if(err) { console.log("post2"); return console.log('error'); } else { console.log('postsuccess'); res.json({response: true}); } }); }; here mongoose file:
var mongoose = require('mongoose'), schema = mongoose.schema, bcrypt = require('bcrypt-nodejs'), salt_work_factor = 10; var userschema = new schema({ email: { type: string, required: true, lowercase:true, index: { unique: true } }, password: { type: string, required: true }, firstname: {type: string, required: true}, lastname: {type: string, required: true}, phone: {type: number, required: true}, birthday: {type: date, required: true}, friendrequest: {type: array}, friend: {type: array} }); userschema.pre('save', function(next) { var user = this; console.log("email exists"); // hash password if has been modified (or new) if (!user.ismodified('password')) return next(); // generate salt bcrypt.gensalt(salt_work_factor, function(err, salt) { if (err) return next(err); // hash password along our new salt bcrypt.hash(user.password, salt, null, function(err, hash) { if (err) return next(err); // override cleartext password hashed 1 user.password = hash; next(); }); }); }); userschema.methods.comparepassword = function(candidatepassword, cb) { bcrypt.compare(candidatepassword, this.password, function(err, ismatch) { if (err) return cb(err); cb(null, ismatch); }); }; module.exports = mongoose.model('user', userschema);
so document mongo finds matching provided userid not have array friendrequest property. @ specific document id in mongo shell , fix friendrequest array.
Comments
Post a Comment