【发布时间】:2018-07-02 10:53:36
【问题描述】:
我的代码
我在后端的代码将获取发布数据并调用异步函数
app.post('/login', async (req, res) => {
var mail = decodeURIComponent(req.body.mail), password;
await bcrypt.hash(decodeURIComponent(req.body.password), saltRounds, function(err, hash) {
...
});
});
问题
如果我想启动应用程序,我会收到以下警告:
[DEP0013] 弃用警告:不使用回调调用异步函数是弃用
有没有更好的解决方案?
编辑
如果我只是这样做,我会收到同样的警告,但我不确定如何在此处进行回调。
app.post('/login', async (req, res) => { res.json({ success: true }); });
【问题讨论】:
-
如果您使用
async关键字,则不需要将回调函数传递给hash方法。await bcrypt.hash(myPlaintextPassword, saltRounds)。如果错误仍然存在,将回调添加到控制器async (req, res, next) => { -
我确信您不必进行任何 URL 解码。
req.body中的所有值都已解码。这样做会导致错误。 -
确保始终将
await包裹在 try/catch 中:app.post('/login', async (req, res, next) => { try { await ... } catch (e) { next(e); } });see -
你为什么要使用 await?
标签: javascript node.js express asynchronous