异步等待没有按预期等待
问题描述:
我发誓我有这个工作,然后改变了一些东西,不能完全得到它。 我有了这段代码明示控制器:异步等待没有按预期等待
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);
};
它总是在控制器抛出一个错误: TypeError: Cannot read property 'then' of undefined
答
你在这里有一个多余的功能,其结果你不return
。只需删除函数并写入
myModule.organizationLookup = async function getOrganization(domain) {
try {
return await thirdpartySDK.Company.find({domain: domain});
} catch (err) {
console.log(err)
}
};
这是因为您没有将任何内容返回给'organizationLookup',您在'getOrganization(domain)'内调用,但未返回该值。 – Gerardo
另外,如果你要返回一个承诺,我也没有看到使用'async/await'的意思。 – Gerardo
实际代码比较复杂,对本文过于简单 – dylan