【问题标题】:"Runtime.UserCodeSyntaxError" while trying to declare an async AWS Lambda function尝试声明异步 AWS Lambda 函数时出现“Runtime.UserCodeSyntaxError”
【发布时间】:2020-11-25 12:16:25
【问题描述】:

我有一个旧的 AWS Lambda 函数,它被声明为同步(使用 Promises),声明如下所示:

exports.getSong = (event, context, callback) => { }

它按预期工作。最近,我决定使用 async/await 重写它,因此尝试异步声明 getSong 函数,如下所示:

exports.getSong = async (event, context) => { }

在尝试执行时,我收到以下错误:

{
  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Unexpected token '.'",
  "trace": [
    "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '.'",
    "    at _loadUserApp (/var/runtime/UserFunction.js:98:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1133:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)",
    "    at Module.load (internal/modules/cjs/loader.js:977:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:877:14)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)",
    "    at internal/main/run_main_module.js:18:47"
  ]
}

从这个错误消息中绝对不清楚问题是什么,但是通过谷歌搜索类似的问题并缩小范围,我发现问题出在函数声明上。

我尝试将getSong 函数声明为同步函数,然后在其中运行另一个异步函数,如下所示:

var anotherAsyncFunction = async () => { }
exports.getSong = (event, context, callback) => { anotherAsyncFunction() }
    

但是,我得到了同样的错误。很明显,它与 Lambda 中的异步函数声明有关。这里可能是什么问题?谢谢。

【问题讨论】:

  • 看来您需要添加更多代码供我们调试(即第 98 行左右)
  • 我没有 98 行代码。这就是为什么我说错误消息没有帮助。
  • 上面的代码是你的 lambda 的唯一代码吗?
  • 您在代码底部的getSong 函数不是异步的...您希望使用await 处理anotherAsyncFunction 还是将.thencallback 结合使用

标签: javascript node.js amazon-web-services aws-lambda async-await


【解决方案1】:

我遇到了类似的问题。对我来说,这是 res.Body?.toString('utf-8'),其中 lambda 无法理解 Body?. 是什么,我认为在你的情况下你应该执行以下操作:

export const getSong = async (event, context) => { ...}

对于遇到同样问题的人来说,找出原因的最好方法是转到您的 lambda 代码页,它应该在代码行上显示一个红色的X

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 2020-01-16
    • 2021-10-05
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 2016-09-18
    相关资源
    最近更新 更多