alm*_*arc 6 javascript amazon-web-services node.js async-await aws-lambda
我有一个旧的 AWS Lambda 函数,它被声明为同步(使用 Promises),声明如下所示:
exports.getSong = (event, context, callback) => { }
Run Code Online (Sandbox Code Playgroud)
它按预期工作。最近,我决定使用 async/await 重写它,因此尝试getSong异步声明该函数,如下所示:
exports.getSong = async (event, context) => { }
Run Code Online (Sandbox Code Playgroud)
在尝试执行时,我收到以下完整错误:
{
"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"
]
}
Run Code Online (Sandbox Code Playgroud)
从这个错误消息中绝对不清楚问题是什么,但是通过谷歌搜索类似的问题并缩小范围,我发现问题出在函数声明上。
我尝试将该getSong函数声明为同步函数,然后在其中运行另一个异步函数,如下所示:
var anotherAsyncFunction = async () => { }
exports.getSong = (event, context, callback) => { anotherAsyncFunction() }
Run Code Online (Sandbox Code Playgroud)
但后来,我得到了同样的错误。很明显,它与 Lambda 内的异步函数声明有关。这里可能有什么问题?谢谢。
我遇到了类似的问题。对我来说res.Body?.toString('utf-8'),lambda 无法理解Body?.我认为在你的情况下你应该执行以下操作:
export const getSong = async (event, context) => { ...}
Run Code Online (Sandbox Code Playgroud)
对于遇到同样问题的人来说,找出原因的最佳方法是转到您的 lambda 代码页,它应该X在代码行上显示红色
| 归档时间: |
|
| 查看次数: |
7677 次 |
| 最近记录: |