猫鼬异步/等待找到然后编辑并保存?

编程入门 行业动态 更新时间:2024-10-25 14:32:04
本文介绍了猫鼬异步/等待找到然后编辑并保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以使用异步/等待承诺进行查找然后保存?

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); }

更多推荐

猫鼬异步/等待找到然后编辑并保存?

本文发布于:2023-11-13 01:04:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并保存   编辑

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!