I have a problem on debugging this error can you help me?
This is my code
router.post('/accounts/show_accounts/:id', function(req,res){
Account.findOne(
{_id:req.params.id},
{$push: {team: {team_name: req.body.team_name}}},
{safe: true, upsert: true},
function(err, model) {
console.log(err);
}
)
});
And I am getting the below error
errmsg: 'Unsupported projection option: $push: { team: { team_name: “hahaha” } }', code: 2, codeName: 'BadValue' }
As the error states, findOne() method considers the document { "$push": { "team": { "team_name": req.body.team_name } } } as a projection and in a projection field names do not start with $. I believe you want to do an update operation, not a query. In that case, you need to use the findOneAndUpdate() or findByIdAndUpdate() method because the $push operator is only used in an update operation, not a query like findOne():
router.post('/accounts/show_accounts/:id', function(req, res){
Account.findByIdAndUpdate(
req.params.id,
{ "$push": { "team": { "team_name": req.body.team_name } } },
{ "upsert": true, "new": true },
function(err, model) {
if (err) throw err;
console.log(model);
}
)
});
NB: findByIdAndUpdate(id, ...) is equivalent to findOneAndUpdate({ _id: id }, ...)
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