简体   繁体   English

为什么await / async的结果未定义?

[英]Why is the result of await/async undefined?

I have a route like: 我有一条路线:

router.get("/my_resource", myController.getResult);

Then my controller is like: 然后我的控制器就像:

getResult: async (req, res, next) => {
    try {
      const param = req.query.param;
      let response = {};
      if (param) {
        let [result1, result2] = await Promise.all([myModel.getResult1(param), myModel.getResult2(param)]);
        console.log(result1);
        console.log(result2);
      }
      res.send(response);
    } catch (e) { 
        next(e);
    }
  }

And my model (which is querying a mongodb) looks like: 我的模型(查询mongodb)看起来像:

getResult1: (param) => {
    new Promise((resolve, reject) => {
      MongoSchema1.findById(param, (error, result) => {
        if (error) return reject(error);
        resolve(result ? result : {});
      }).select("field1 field2");
    })
  }

getResult2: (param) => {
  new Promise((resolve, reject) => {
    MongoSchema2.findById(param, (error, result) => {
      if (error) return reject(error);
      resolve(result ? result : {});
    }).select("field1 field2");
  })
}

Here, when I try to console.log() in the model functions I can see that the result is not undefined . 在这里,当我在模型函数中尝试console.log() ,我可以看到结果undefined I get the correct result. 我得到了正确的结果。 However, I can see that the console.log() in the controller method gets executed before the console.log() in the model methods. 然而,我可以看到console.log()在控制器的方法获取之前执行console.log()在模型中的方法。

In the code above, I'm making the async calls in parallel with Promise.all() . 在上面的代码中,我正在与Promise.all()并行进行异步调用。 However, even if I try to run one at a time, I still get my result to be undefined in the controller method. 但是,即使我尝试一次运行一个,我仍然得到我的结果在控制器方法中未定义。 What am I doing wrong? 我究竟做错了什么? Do I need import any module before I can await ? await之前,我是否需要导入任何模块? Thanks. 谢谢。

The problem is that your methods getResult1 and getResult2 do not return Promises objects. 问题是你的方法getResult1getResult2不返回Promises对象。

Replace 更换

  getResult1: (param) => {
    new Promise((resolve, reject) => {
      MongoSchema1.findById(param, (error, result) => {
        if (error) return reject(error);

        resolve(result ? result : {});
      }).select("field1 field2");
    })
  }

By 通过

  getResult1: (param) => {
    return new Promise((resolve, reject) => {
      MongoSchema1.findById(param, (error, result) => {
        if (error) return reject(error);
        resolve(result ? result : {});
      }).select("field1 field2");
    });
  }

Or 要么

  getResult1: param => new Promise((resolve, reject) => {
      MongoSchema1.findById(param, (error, result) => {
        if (error) return reject(error);

        resolve(result ? result : {});
      }).select("field1 field2"));
  }

Or 要么

getResult1: async param => (await MongoSchema1.findById(param).select('field1 field2')) || {};

Or 要么

getResult1: async param => (await MongoSchema1.findById(param, 'field1 field2')) || {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM