小编Luc*_*ama的帖子

Mongoose async/await with Koa 卡在 await Model.findOne().exec()

我有一个 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();永远不会返回并卡住。

我认为问题出在那里,因为如果我删除,代码会正常执行。如果我切换到 …

mongoose node.js async-await babeljs koa2

6
推荐指数
1
解决办法
2549
查看次数

标签 统计

async-await ×1

babeljs ×1

koa2 ×1

mongoose ×1

node.js ×1