【发布时间】:2016-07-19 13:44:34
【问题描述】:
我正在使用 MEAN 堆栈构建后端,但是当我尝试更新数据库中的文档时出现错误:
topUp = function(name, amount, callback) {
User.updateOne(
{ "name" : name },
{ $set: { "wallet": amount } },
function(err, results) {
console.log(results);
callback();
});
};
TypeError: User.updateOne 不是函数
但是例如findOne() 工作正常:
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();
});
}
});
“用户”是一个 Mongo 模型文件。
【问题讨论】:
-
因为
findOne是一个预定义函数,而updateOne()不是。默认情况下,它应该只更新一条记录。您可以使用multi: true更新多条记录。 -
@MohitBhardwaj 好吧,根据 Mongo 文档 updateOne() 也是预定义的:proof
-
我认为您可能正在使用的数据库驱动程序中没有定义它。我认为您正在使用 Mongoose 并且
updateOne()在那里不可用。您不能将所有本机 mongodb 函数与所有驱动程序一起使用。 -
我不是很确定,但根据我的理解是这样的。
-
@MohitBhardwaj 哇,非常感谢,这是真的!我应该使用 Mongoose 支持的 update() 而不是 updateOne()。如果您将其写为答案,我将很高兴接受它:)
标签: javascript node.js mongodb express