node.js - findOneAndUpdate with push array elements gives error in mongoose -
my query shown below:
const updatelikes = (item_id, userinfo) => { return new promise((resolve, reject) => { itemlike.findoneandupdate({ 'item_id': item_id }, { $inc: { no_of_likes: 1 } }, { "$push": { "users": userinfo } }, { 'new': true }, (err, info) => { if (err) { reject(err); } else { if (info) { resolve(); } else { reject('no item found'); } } }); }); };
itemlike.js
const itemlike = new schema({ item_id: { type: mongoose.schema.objectid, ref: 'items', index: true }, no_of_likes: { type: number, default: 0 }, users: [{ type: mongoose.schema.objectid, ref: 'user' }] }, { versionkey: false }); module.exports = mongoose.model('item_like', itemlike);
as execute query , error shown below:
events.js:160 throw er; // unhandled 'error' event ^ typeerror: callback.apply not function @ query.<anonymous> (c:\users\anand\quflipapi\node_modules\mongoose\lib\model.js:3702:16) @ c:\users\anand\quflipapi\node_modules\kareem\index.js:273:21 @ c:\users\anand\quflipapi\node_modules\kareem\index.js:127:16 @ _combinedtickcallback (internal/process/next_tick.js:67:7) @ process._tickdomaincallback (internal/process/next_tick.js:122:9)
am missing here?
looking @ docs findoneandupdate, think you're providing 1 many parameters.
it should conditions, update, options, callback
. you're getting error because mongoose trying invoke { 'new': true }
callback function.
it looks update definition has been split 2 objects mistake. need remove braces highlighted in bold below.
{ $inc: { no_of_likes: 1 } }, { "$push": { "users": userinfo } }
Comments
Post a Comment