【问题标题】:Azure function complains of JS syntax error when syntax is validAzure 函数在语法有效时抱怨 JS 语法错误
【发布时间】:2018-05-11 02:36:46
【问题描述】:

我在 JS 中定义了一个 Azure 函数

module.exports = async function (context, req) {
    if (req.query.name || (req.body && req.body.name)) {

        // generate mock result
        const mockChecker = new mockCheckBuild();
        const result = await mockChecker.runAsync();

        context.res = {
            body: result
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
    context.done();
};

function mockCheckBuild() {
   this.statuses = ['Failed', 'Complete'];

   this.branchId = 808;

   this.buildNumbers = ['0.1.1.1023', '0.1.1.1024', '0.1.1.1025'];

   this.runAsync = async function() {
      return new Promise(resolve => 
        setTimeout(() =>
            resolve({
                branchId: this.branchId,
                latestBuild: this.statuses.randomElement(),
                buildStatus: this.buildNumbers.randomElement()
            })
        , 2000)
      );
   };

   return this;
}

Array.prototype.randomElement = function()
{
    const index = Math.floor(Math.random() * this.length);
    return this[index];
};

我已经通过许多语法验证器来验证它是正确的 JavaScript。我还要注意 Azure 语法高亮显示像 asyncconst 这样的词。

但是,当我运行它时,我得到了

"执行函数时出现异常:Functions.CheckLatestBuild -> One 或更多错误发生。 -> D:\home\site\wwwroot\CheckLatestBuild\index.js:1\n(函数 (exports, require, module, __filename, __dirname) { module.exports = 异步函数(上下文,请求){\n
^^^^^^^^\n\nSyntaxError: Unexpected token function\n at createScript (vm.js:56:10)\n 在 Object.runInThisContext (vm.js:97:10)\n 在模块._

知道为什么吗?或者关于如何更好地调查的任何建议?

【问题讨论】:

    标签: javascript azure ecmascript-6 azure-functions


    【解决方案1】:

    Azure Functions v1 运行不支持异步的 Node 6.x。如果您改为尝试 Functions v2 Preview,则可以运行 Node 8.x(以及很快的 10.x),并且异步将起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多