简体   繁体   English

Typescript中解析Uint8Array时Null

[英]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.

相关问题 即使数组不为空且 state 不是 null,列表也不会呈现 - List not rendering even when the array is not empty and the state is not null SQL 数组与 Null - SQL Array with Null SQL - 使用“IGNORE NULLS”时数组中包含的 NULL 值 - SQL - NULL values included in array when using 'IGNORE NULLS' 第 0 行:解析错误:无法读取未定义的属性“映射”TypeScript React - Line 0: Parsing error: Cannot read property 'map' of undefined TypeScript React 当 firestore 中有数据时检索到 null flutter - Retrieved null when there is data in firestore with flutter 使用 graphql 查询时如何解决验证错误? (打字稿) - How to resolve validation error when querying with graphql? (typescript) firebase 文档 当文档中存在 map 和数组时,某些字段显示为 null - firebase document some fields are showing as null when map and array exist in the document 尝试在 swift 应用程序中获取数据后,数组为 null - Array is null after trying to fetch data in swift app Typescript 类型为“用户 | 的参数”错误 null' 不可分配给 'User' 类型的参数 - Typescript error Argument of type 'User | null' is not assignable to parameter of type 'User' ImageCropper 与 Uint8List 文件一起使用 -flutter - ImageCropper use with Uint8List file -flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM