admin管理员组文章数量:1387412
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 butupdateOne()
is not. It should by default update only one record. You can usemulti: 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
2 Answers
Reset to default 4I 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.
本文标签: javascriptWhy getting error when updating MongoDbStack Overflow
版权声明:本文标题:javascript - Why getting error when updating MongoDb? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744542795a2611714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论