DynamoDB 文档客户端将字符串集 (SS) 属性作为对象返回



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

查询:

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

预期:

{ 
"user": "boris",
"phones": ["+23xxxxx999", "+23xxxxx777"]
}

实际:

{ 
"user": "boris",
"phones": {
"type": "String",
"values": ["+23xxxxx999", "+23xxxxx777"],
"wrapperName": "Set"
}
}

谢谢!

来自 [AWS.DynamoDB.Converter] 是一种解决方案,如果您的数据来自例如:

{
"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"
}
}
}

请注意每个属性的对象/映射 {} 规范,其中包含 attr 类型。 表示您使用的是 [dynamodb] 类,而不是 [DynamoDB.DocumentClient]。 [unmarshall] 会将 DynamoDB 记录转换为 JavaScript 对象。

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

尽管如此,我还是面临着与您完全相同的用例。只有一个属性,在我的情况下是类型集(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
});

干杯 哈姆雷特

最新更新