猫鼬“更新”不起作用,除非我使用“ .then”

我试图用猫鼬在数据库上编辑实例,除非我在函数之后使用then,否则update和updateone函数对我不起作用

此代码有效:

Word.findOne({word:req.body.word}).then(function (res) {
    if (res === null) {
        const new_word = new Word({
            word: req.body.word,translate:  req.body.translation
        });
        new_word.save();
    }
    else
        res.updateone({translate: req.body.translation}).then(function () {});
});

此代码无效:

Word.findOne({word:req.body.word}).then(function (res) {
    if (res === null) {
        const new_word = new Word({
            word: req.body.word,translate:  req.body.translation
        });
        new_word.save();
    }
    else
        res.updateone({translate: req.body.translation});
});

预先感谢您:)

lumnstar 回答:猫鼬“更新”不起作用,除非我使用“ .then”

也许尝试使用.exec

res.updateOne({translate: req.body.translation}).exec();
本文链接:https://www.f2er.com/3095983.html

大家都在问