考阿的问题,猫鼬等待不返回ctx.body

我正在使用koa重置密码,想要使用.save来启动schema.pre('save')。

数据通过findOneAndUpdate返回,但是当我使用.save时却没有。

通过await / asyncs正确返回.save文档的神奇组合是什么?

r.post("/public/auth/resetpass",async (ctx,next) => {
  const values = ctx.request.body;
  const query = {
    email: values.email,resetPasswordToken: values.resetPasswordToken,resetPasswordExpires: {
      $gt: new Date(new Date())
    }
  };

  const update = {
    password: values.password,resetPasswordToken: null,resetPasswordExpires: null
  };

//  let userFound = null;

  await User.findOne(query,async function(err,user) {
    if (err) {
      console.log("*** err");
      next(err);
    } else {
      if (_.isEmpty(user)) {
        ctx.status = 200;
        ctx.body = {
          error: true,message: "token is incorrect or time has expired for password reset"
        };
      } else {
        user.password = values.password;
        await user.save(function(err,doc) {
          if (err) {
            console.log('***err saving');
            next(err);
          } else {
            //console.log fires,but ctx body doesn't return
            console.log ('***saved,writing poco');
            ctx.body = userToPoco(doc);
          }
        });
      }
    }
  });
});

weichaochenweichao 回答:考阿的问题,猫鼬等待不返回ctx.body

最终变成了诺言。

  await user.save().then (doc =>{
    ctx.body = doc;
  });
本文链接:https://www.f2er.com/2682163.html

大家都在问