javascript - Async Await 未按预期等待

标签 javascript node.js asynchronous

我发誓我已经做到了这一点,然后改变了一些东西,但不太明白。 我有一个带有这段代码的快速 Controller :

router.post('/', (req, res, next) => {
  myModule.organizationLookup(req.body.domain).then((data) => {
    res.status(200).send(data);
  })
});

然后我的问题代码:myModule.js 看起来像这样:

myModule.organizationLookup = (domain) => {
  async function getOrganization(domain) {
    try {
      return await thirdpartySDK.Company.find({domain: domain});
    }
    catch (err) {
      console.log(err)
    }
  }
  getOrganization(domain);
};

它总是在 Controller 中抛出错误: 类型错误:无法读取未定义的属性“then”

最佳答案

其中有一个多余的函数,您不会返回其结果。只需删除该函数并编写

myModule.organizationLookup = async function getOrganization(domain) {
  try {
    return await thirdpartySDK.Company.find({domain: domain});
  } catch (err) {
    console.log(err)
  }
};

关于javascript - Async Await 未按预期等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45497159/

相关文章:

检查元音的 JavaScript 函数

javascript - 不确定如何将请求转发到另一个路由器?

javascript - Node.js 变量在函数之外使用

选择/轮询与异步 I/O 的性能

c++ - 从编译时依赖图 (DAG) 构建异步 `future` 回调链

javascript - React-Native:访问另一个类中的 TextField 值

javascript - 过滤逻辑过于严格

javascript - 在javascript中使用正则表达式获取引号外的代码

node.js - 如何将 React 中的默认端口从 3000 更改为另一个端口?

javascript - 调用 FatSecret Sharp API 此时无法启动异步操作