本文介绍了猫鼬异步/等待找到然后编辑并保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用异步/等待承诺进行查找然后保存?
Is it possible to do a find then save using async/await promise?
我有以下代码:
try { var accounts = await Account.find() .where("username").in(["email@gmail"]) .exec(); accounts.password = 'asdf'; accounts.save(); } catch (error) { handleError(res, error.message); }并且出现以下错误:
ERROR: accounts.save is not a function推荐答案
这就是我想要的:
try { var accounts = await Account.findOneAndUpdate( {"username" : "helllo@hello"}, {$set: {"password" : "aaaa"}}, {new : true} ); res.status(200).json(accounts); } catch (error) { handleError(res, error.message); }或(感谢@JohnnyHK的find vs findOne技巧!)
or (thanks @JohnnyHK for the find vs findOne tip!):
try { var accounts = await Account.findOne() .where("username").in(["hello@hello"]) .exec(); accounts.password = 'asdf'; accounts.save(); res.status(200).json(accounts); } catch (error) { handleError(res, error.message); }更多推荐
猫鼬异步/等待找到然后编辑并保存?
发布评论