最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - errmsg: 'Unsupported projection option: $push: { ... }', code: 2, codeName: 'BadValue&#

programmeradmin4浏览0评论

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' }

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' }

Share Improve this question edited Jan 12, 2017 at 6:58 chridam 104k26 gold badges243 silver badges241 bronze badges asked Jan 11, 2017 at 12:59 Joel RalphJoel Ralph 531 silver badge8 bronze badges 1
  • 3 Please post the code and not an image – jean-max Commented Jan 11, 2017 at 12:59
Add a ment  | 

1 Answer 1

Reset to default 8

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 }, ...)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论