boooni boooni - 3 months ago 9
Node.js Question

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.

Answer

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