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

javascript - Why getting error when updating MongoDb? - Stack Overflow

programmeradmin4浏览0评论

I am building backend with MEAN stack, but when I try to update document in the db i am getting an error:

 topUp = function(name, amount, callback) {
    User.updateOne(
        { "name" : name },
        { $set: { "wallet": amount } },
        function(err, results) {
            console.log(results);
            callback();
        });
 };

TypeError: User.updateOne is not a function

But e.g. findOne() works fine:

    User.findOne({
                name: decoded.name
            }, function(err, user) {
                if (err) throw err;

                i

f (!user) {
                return res.status(403).send({success: false, msg: 'Authentication failed. User not found.'});
            } else {
                //res.json({success: true, info: {wallet: user.wallet, userPic: user.userPic}});
                topUp(decoded.name, amount, function() {
                    User.close();
                });
            }
        });

"User" is a Mongo model file.

I am building backend with MEAN stack, but when I try to update document in the db i am getting an error:

 topUp = function(name, amount, callback) {
    User.updateOne(
        { "name" : name },
        { $set: { "wallet": amount } },
        function(err, results) {
            console.log(results);
            callback();
        });
 };

TypeError: User.updateOne is not a function

But e.g. findOne() works fine:

    User.findOne({
                name: decoded.name
            }, function(err, user) {
                if (err) throw err;

                i

f (!user) {
                return res.status(403).send({success: false, msg: 'Authentication failed. User not found.'});
            } else {
                //res.json({success: true, info: {wallet: user.wallet, userPic: user.userPic}});
                topUp(decoded.name, amount, function() {
                    User.close();
                });
            }
        });

"User" is a Mongo model file.

Share Improve this question asked Jul 19, 2016 at 13:44 boooniboooni 1531 gold badge3 silver badges10 bronze badges 6
  • because findOne is a predefined function but updateOne() is not. It should by default update only one record. You can use multi: true to update multiple records. – Mohit Bhardwaj Commented Jul 19, 2016 at 13:46
  • @MohitBhardwaj well, according to Mongo docs updateOne() is predefined too: proof – boooni Commented Jul 19, 2016 at 13:49
  • I think it's not defined in the database driver that you might be using. I think you are using Mongoose and updateOne() is not available there. You cannot use all native mongodb functions with all drivers. – Mohit Bhardwaj Commented Jul 19, 2016 at 13:59
  • I am not very sure about it, but that is so as per my understanding. – Mohit Bhardwaj Commented Jul 19, 2016 at 13:59
  • 1 @MohitBhardwaj wow, thanks a lot, that's true! I should have used update() which is supported by Mongoose instead of updateOne(). If you write it as an answer I will be glad to accept it:) – boooni Commented Jul 19, 2016 at 14:25
 |  Show 1 more ment

2 Answers 2

Reset to default 4

I think it's not defined in the database driver that you might be using. I think you are using Mongoose and updateOne() is not available there. You cannot use all native mongodb functions with all drivers

There is an en existing enhancement request for this https://github./Automattic/mongoose/issues/3997 , but maybe the findByIdAndUpdate() method could be a close alternative.

发布评论

评论列表(0)

  1. 暂无评论