gpt4 book ai didi

javascript - 当语法有效时,Azure 函数提示 JS 语法错误

转载 作者:行者123 更新时间:2023-11-29 10:59:22 24 4
gpt4 key购买 nike

我有一个用 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 之类的单词。

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

"Exception while executing function: Functions.CheckLatestBuild -> One or more errors occurred. -> D:\home\site\wwwroot\CheckLatestBuild\index.js:1\n(function (exports, require, module, __filename, __dirname) { module.exports = async function (context, req) {\n
^^^^^^^^\n\nSyntaxError: Unexpected token function\n at createScript (vm.js:56:10)\n at Object.runInThisContext (vm.js:97:10)\n at Module._

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

最佳答案

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

关于javascript - 当语法有效时,Azure 函数提示 JS 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com