boooni
boooni

Reputation: 153

Why getting error when updating MongoDb?

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.

Upvotes: 3

Views: 7989

Answers (2)

Andre M
Andre M

Reputation: 7534

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

Upvotes: 0

Mohit Bhardwaj
Mohit Bhardwaj

Reputation: 10083

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

Upvotes: 4

Related Questions