我为我的站点创建了一个注册系统,它向注册用户发送一封确认电子邮件,他们的确认代码是他们在 mongoose 数据库中的帐户的 _id
。
然后,回到我的网站,他们输入他们的确认码,我想使用 .update()
方法来更改 confirmed
属性的值该用户的数据库中的 0
到 1
。
这是我的代码:
router.post('/confirmAcc',function(req, res, next) {
var getid=req.body.confirmcode;
console.log(getid);
var requis = { "_id": getid };
User.update(requis, {confirmed: '1'}, function (err, doc){
console.log(doc);
if (err) throw err;
});
res.render('index', {title:'nDoto'});
});
数据库匹配id
没有问题,但是没有更新现有的“confirmed
”属性为“1
”。
我不知道为什么。
此外,当我运行代码并输入我的确认代码并且该函数运行时,控制台写道:
{ ok: 0, n: 0, nModified: 0 }
最佳答案
我解决了这个问题。我没有使用 User.update(),而是使用了:
router.post('/confirmAcc',function(req, res, next) {
var getid=req.body.confirmcode;
var requis = { "_id": getid };
User.findOne(requis, function (err, doc){
doc.credentials.confirmed="1";
doc.save();
if (err) throw err;
});
res.render('index', {title:'nDoto'});
});
这有效:)
关于javascript - 无法使用 .update() 更新 Mongoose 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34968055/