[英]Null when parsing Uint8Array in Typescript
I have the following object that I get as response when calling aws Lambda client from nodeJS.当从 nodeJS 调用 aws Lambda 客户端时,我得到以下 object 作为响应。
{
'$metadata': {
httpStatusCode: 200,
requestId: '1245',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
ExecutedVersion: '$LATEST',
Payload: Uint8Array(4) [ 110, 119, 106, 108 ],
StatusCode: 200
}
Below is the piece of code I have to read the payload array.下面是我必须读取有效负载数组的一段代码。
const res = await myclient.send(command);
const error = res.FunctionError;
console.log("res", res);
if (error) {
throw new Error(`Error invoking lambda:${error}`);
}
if (!res.Payload) {
throw new Error('Payload is empty');
}
const resultString = JSON.parse(new TextDecoder('utf-8').decode(res.Payload));
console.log("resultString", resultString);
However when the code runs, I get resultString null
.但是,当代码运行时,我得到
resultString null
。 What am I doing wrong here?我在这里做错了什么? How can I correctly read the payload array?
如何正确读取有效负载数组? Appreciate any advice.
感谢任何建议。
Not sure what is going on.不确定发生了什么。 I tried a simple example to see what was going on.
我尝试了一个简单的例子来查看发生了什么。
const array = new Uint8Array(4);
array[0] = 110;
array[1] = 119;
array[2] = 106;
array[3] = 108;
const res = new TextDecoder('utf-8').decode(array);
console.log(res); // nwjl
So if the data you are getting is correct, omitting the JSON.parse
gives me a string at least.所以如果你得到的数据是正确的,省略
JSON.parse
至少给我一个字符串。 But including it throws an error.但是包括它会引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.