首页 > 解决方案 > 我传递的函数并没有被 azure function app 运行

问题描述

我正在尝试让我的函数应用程序将 dta 记录到 azure 表中。我按照示例进行了操作,但是未运行我作为回调提供的函数。

我已经尝试在我的机器上本地的节点服务器上运行它并且它工作正常,因此我认为这是 azure 的问题?我还尝试创建一个没有不必要节点包的新函数应用程序,以查看它们是否是问题所在,但这没有帮助。

module.exports = async function(context, req) {

  var tableSvc = azure.createTableService('account name', 'access key');
  tableSvc.createTableIfNotExists('mytable', function(error, result, response) {
    context.log("Reached this point");
    if (!error) {
      context.log(result);
    }
  });

}

我希望在函数应用日志中看到“到达这一点”以及结果——如果没有错误的话。

标签: javascriptnode.jsfunctionazure

解决方案


该功能在您将请求发送到存储之后但在您点击之前完成,context.log("Reached this point");因此即使正在创建表也不会发生日志记录。由于createTableIfNotExists()不返回可以等待的承诺,因此您需要同步运行该函数并使用它context.done()来让运行时知道该函数何时完成。

module.exports = function(context, req) {

var tableSvc = azure.createTableService('account name', 'access key');

tableSvc.createTableIfNotExists('mytable', function(error, result, response) {
  context.log("Reached this point");
  if (!error) {
    context.log(result);
  }

  context.done();
  });
}

推荐阅读