[英]Serverless invoke local data type problem
當我運行此本地調用時,在我的 NodeJS lambda 中,有效負載以object
而不是string
的形式出現
serverless invoke local -f MyfunctionName --data '{ "data": "hello world" }'
是否有一個原因? 如何將此 json 有效負載作為字符串傳遞?
根據AWS 文檔:
運行時將三個 arguments 傳遞給處理程序方法。 第一個參數是事件 object,它包含來自調用者的信息。 調用程序在調用 Invoke 時將此信息作為 JSON 格式的字符串傳遞,運行時將其轉換為 object。 當 AWS 服務調用您的 function 時,事件結構因服務而異。
由於處理程序期望 event 是“JSON 格式的字符串”,那么這個字符串將被轉換為 object 是正常的。
與其傳遞原始 JSON 並期望將其解析為字符串,不如將其簡單地包裝到 JSON 字段之一中,例如:
serverless invoke local -f MyfunctionName --data '{"inputJson": "{ \"data\": \"hello world\" }"}'
對於這個輸入和這個處理程序:
export const handler = async (event, context) => {
console.log('type of event.inputJson = ' + typeof event.inputJson)
console.log('value of event.inputJson = ' + event.inputJson)
const parsedInputJson = JSON.parse(event.inputJson)
console.log('value of inputJson.data = ' + parsedInputJson.data)
};
我收到以下 output:
type of event.inputJson = string
value of event.inputJson = { "data": "hello world" }
value of inputJson.data = hello world
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.