Azure 函数打字稿未正确等待方法调用



我正在研究一个azure函数,它接收更新azure cosmos数据库的请求。因此,在接收到请求后,函数从数据库中读取项目,如果项目存在,则相应地更新。但是它不像我想的那样工作,当函数运行时阅读日志,似乎函数不会等到从数据库读取完成。下面是代码片段:

const SaasWebhook: AzureFunction = function (context: Context, req: HttpRequest): Promise<void> {
return new Promise(async (resolve, reject) => {
// Initiate azure cosmos db
// Get the payload information
const subscriptionId: string = req.body?.subscriptionId;
const action: string = req.body?.action;
const planId: string = req.body?.planId;
const quantity: string = req.body?.quantity;
let patchedItem: any;
context.log(req.body);
try {
// Read subscription info from the storage
const item: Item = container.item(subscriptionId, subscriptionId);
const { resource: readItem } = await item.read();
context.log("Item from read: ");
context.log(readItem);
// Check the result, data stored in the container is formatted as 
if (readItem.id) {
// UPDATE ITEM here
try {
// Try to write the update to the database
const { resource: updatedItem } = await container
.item(id, id)
.replace(patchedItem);
// Give success response
context.log("Operation success. Updated Item: ");
context.log(updatedItem);
context.res = {
status: 200, /* Return error status */
body: "Operation success."
};
resolve();
} catch (error) {
context.res = {
status: 500, /* Return error status */
body: "Operation failure: Item replace failed."
};
reject(error);
}
}
} catch (error) {
context.log("Internal error: failure on accessing Database");
context.log(error);
context.res = {
status: 500, /* Return error status */
body: "Internal error: failure on accessing Database"
};
reject(error);
}
});
}

在记录器上,我将打印这些消息:

2021-02-05T08:14:57.938 [Information] {THE ITEM FROM DATABASE}
2021-02-05T08:14:58.118 [Information] Item from read:
2021-02-05T08:14:58.118 [Information] undefined
2021-02-05T08:14:58.118 [Information] Internal error: failure on accessing Database
2021-02-05T08:14:58.118 [Information] TypeError: Cannot read property 'id' of undefinedat Object.<anonymous> (D:homesitewwwrootdistSaasWebhookindex.js:43:26)at Generator.next (<anonymous>)at fulfilled (D:homesitewwwrootdistSaasWebhookindex.js:5:58)at processTicksAndRejections (internal/process/task_queues.js:97:5)
2021-02-05T08:14:58.120 [Error] Executed 'Functions.SaasWebhook' (Failed, Id=ec15bf1b-d9d5-4ebc-900f-73cbc0976b41, Duration=188ms)Result: FailureException: TypeError: Cannot read property 'id' of undefinedStack: TypeError: Cannot read property 'id' of undefinedat Object.<anonymous> (D:homesitewwwrootdistSaasWebhookindex.js:43:26)at Generator.next (<anonymous>)at fulfilled (D:homesitewwwrootdistSaasWebhookindex.js:5:58)at processTicksAndRejections (internal/process/task_queues.js:97:5)

作为上下文,该函数被用作Microsoft marketplace SaaS实现API所需的webhook,因此如果调用失败,则在一定时间内再次调用该函数,{the ITEM FROM DATABASE}日志可能来自该函数的前一次调用。该函数用typescript编写,并通过Visual Studio Code上的azure function扩展上传/部署到azure函数中。我读过Typescript函数实际上在部署之前被编译成javascript函数,我注意到在javascript函数上的'await'调用被更改为'yield',这是否有某种关联?这是我想要的js代码的一部分:

try {
// Read subscription info from the storage
const item = container.item(id, id);
const { resource: readItem } = yield item.read();
context.log("Item from read: ");
context.log(readItem);
...
// The rest doesn't have much difference.
...

我也试过使用item.read().then(…)而不是await并得到相同的结果。我正在考虑用纯Javascript重写函数,但我想先听听你的意见。

最后,我用javascript重写了这个函数,它按预期工作了。我猜这是由于函数编译成javascript,从'await'到'yield'的变化与函数的流程混乱。

最新更新