简体   繁体   English

DynamoDB 中字符串集的解组失败

[英]Unmarshall of string set in DynamoDB fails

I have tried to marshall DynamoDB's String Set and unmarshall it back as below.我尝试marshall DynamoDB 的String Set并将其unmarshall组,如下所示。

import {marshall, unmarshall} from '@aws-sdk/util-dynamodb';

test('Marshall and Unmarshall Test', () => {
  const raw = {
    'anArray': new Set([
      'Apple',
      'Mango'
    ])
  };

  console.log(JSON.stringify(marshall(raw)));
  // {"anArray":{"SS":["Apple","Mango"]}}

  const marshalledResult = marshall(raw);

  console.log(JSON.stringify(unmarshall(marshalledResult)));
  // {"anArray":{}}

});

But I am not able to get the String Set back.但我无法取回String Set How should we be doing this when we should get a String Set from dynamo db and use it?当我们应该从 dynamo db 获取一个String Set并使用它时,我们应该怎么做呢?

unmarshall is the right approach. unmarshall是正确的方法。 I believe you are not seeing the expected result because JSON.stringify does not play nicely with sets .我相信您没有看到预期的结果,因为JSON.stringify 不能很好地处理 sets Try logging the unmarshalled result directly:尝试直接记录解组结果:

const unmarshalled = unmarshall(marshalledResult)
console.log(unmarshalled);
// => { anArray: Set(2) { 'Apple', 'Mango' } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM