DynamoDB DocumentClient 将字符串集 (SS) 属性作为对象返回

bor*_*ete 5 amazon-dynamodb aws-lambda aws-sdk-nodejs

我是 DynamoDB 的新手。当我使用 AWS.DynamoDB.DocumentClient 类从表中读取数据时,查询有效,但我得到的结果格式错误。

询问:

{
  TableName: "users",
  ExpressionAttributeValues: {
    ":param": event.pathParameters.cityId,
    ":date": moment().tz("Europe/London").format()
  },
  FilterExpression: ":date <= endDate",
  KeyConditionExpression: "cityId = :param"
}
Run Code Online (Sandbox Code Playgroud)

预期的:

{ 
  "user": "boris",
  "phones": ["+23xxxxx999", "+23xxxxx777"]
}
Run Code Online (Sandbox Code Playgroud)

实际的:

{ 
  "user": "boris",
  "phones": {
     "type": "String",
     "values": ["+23xxxxx999", "+23xxxxx777"],
     "wrapperName": "Set"
  }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 0

如果您的数据如下所示, [ AWS.DynamoDB.Converter ] 中的[ unmarshall ] 函数是一种解决方案:

{
    "Attributes": {
      "last_names": {
        "S": "UPDATED last name"
      },
      "names": {
        "S": "I am the name"
      },
      "vehicles": {
        "NS": [
          "877",
          "9801",
          "104"
        ]
      },
      "updatedAt": {
        "S": "2018-10-19T01:55:15.240Z"
      },
      "createdAt": {
        "S": "2018-10-17T11:49:34.822Z"
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意每个属性的 object/map {} 规范,保存 attr 类型。意味着您正在使用 [ dynamodb ] 类而不是 [ DynamoDB.DocumentClient ]。[ unmarshall ] 会将 DynamoDB 记录转换为 JavaScript 对象。

由 AWS 声明和支持。参考号 https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Converter.html#unmarshall-property

尽管如此,我遇到了与您完全相同的用例。在我的例子中,只有一个属性,即 TYPE SET (NS),我必须手动执行此操作。接下来是一个片段

 // Please notice the <setName>, which represents your set attribute name
ddbTransHandler.update(params).promise().then((value) =>{
      value.Attributes[<setName>] = value.Attributes[<setName>].values;
      return value; // or value.Attributes
});
Run Code Online (Sandbox Code Playgroud)

干杯,哈姆雷特