【问题标题】:findAndModify() giving exception in mongoosefindAndModify() 在猫鼬中给出异常
【发布时间】:2015-02-12 23:23:14
【问题描述】:

我有以下代码来更新子集合字段中的文档,并且我得到了代码后面的异常。

router.route('/user/:_id/host/:_id/accept/').post(function(req, res) {
    Host.findAndModify({
        "query" : {
            "_id" : req.body.eventid,
            "joinees" : {
                "$elemMatch" : {
                    "userid" : req.body.userid
                }
            }
        },
        "update" : {
            "$set" : {
                "joinees.$.status_code" : 1
            },
        },
        "new" : true,
        "upsert" : false
    }, function(err, host) {
        if (err) {
            console.log(err);
            res.json(err);
        } else {
            console.log("kanishka");
            res.json(host);

        }
    });
});

控制台打印的异常如下:

TypeError: undefined is not a function
    at Object.handle (/node/Playo1/server1.js:332:7)
    at next_layer (/node/Playo1/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/node/Playo1/node_modules/express/lib/router/route.js:107:5)
    at /node/Playo1/node_modules/express/lib/router/index.js:195:24
    at param (/node/Playo1/node_modules/express/lib/router/index.js:268:14)
    at param (/node/Playo1/node_modules/express/lib/router/index.js:280:16)
    at param (/node/Playo1/node_modules/express/lib/router/index.js:280:16)
    at Function.proto.process_params (/node/Playo1/node_modules/express/lib/router/index.js:296:3)
    at next (/node/Playo1/node_modules/express/lib/router/index.js:189:19)
    at next (/node/Playo1/node_modules/express/lib/router/index.js:166:38)

【问题讨论】:

  • .findAndModify() 不是猫鼬模型的方法。请参阅.findOneAndUpdate().findByIdAndUpdate(),它们是底层驱动程序方法的特殊用途包装器。
  • @NeilLunn:谢谢 Buddy...它成功了...

标签: node.js mongodb mongoose mongodb-query nosql


【解决方案1】:

试试这个。这将起作用。

router.route('/user/:_id/host/:_id/accept/')
.post(function(req, res) {

        Host.findOneAndUpdate({
                "_id" : req.body.eventid,
                "joinees" : {
                        "$elemMatch" : {
                                "userid" : req.body.userid
                        }
                }
        }, {
                "$set" : {
                        "joinees.$.status_code" : 1
                }
        }, {
                "new" : true,
                "upsert" : false
        }, function(err, host) {
                if (err) {
                        console.log(err);
                        res.json(err);
                } else {
                        res.json({
                                message : "request Accepted",
                                requestStatus : 1
                        });
                }
        });
});

【讨论】:

  • 我正在尝试这个。非常感谢。
  • 不适合我,同样的错误TypeError: Collections.findOneAndUpdate is not a function
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 2018-09-13
  • 2018-08-30
  • 2015-10-23
  • 2020-11-19
  • 2019-05-19
  • 2018-06-18
  • 2018-06-09
相关资源
最近更新 更多