Luc*_*ama 6 mongoose node.js async-await babeljs koa2
我有一个 Koa 2 应用程序,并且 /signup 的帖子由此函数处理:
import User from 'models/user';
export const signup = async (ctx, next) => {
const { email, password } = ctx.request.body;
try {
const existingUser = await User.findOne({ email });
if (existingUser) {
ctx.body = { error: 'Email is in use' };
return next();
}
const user = new User({
email,
password,
});
await user.save();
ctx.body = { success: true };
} catch (e) {
next(e);
}
return next();
};
Run Code Online (Sandbox Code Playgroud)
该函数接收到正确的数据,但await User.findOne().exec();永远不会返回并卡住。
我认为问题出在那里,因为如果我删除,代码会正常执行。如果我切换到 Promise 之类的......find().then它也能工作。async/await 也可以工作,因为如果我更改为 await fetch() (模拟异步)它可以工作……但这是我的 babel 配置
{
"presets" : ["latest", "stage-0"],
"plugins": [
["module-resolver", {
"root": ["./src"]
}]
]
}
Run Code Online (Sandbox Code Playgroud)
猫鼬是版本 4.7.0
| 归档时间: |
|
| 查看次数: |
2549 次 |
| 最近记录: |