javascript DynamoDB,数组的批处理写入项- AWS V3

vwkv1x7d  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(101)

我有一个数组,用来创建batchwriteitem的put。

const people = [{
        location: 'London',
        name: 'Tony Wilson',
        pets:  [ {name: 'Cuddles', age: 4}, { name: 'Jess', age: 2}]
    },
        {
            location: 'Manchester',
            name: 'Liz Smith',
            pets:  [ {name: 'Fluffy', age: 4}, { name: 'Keith the cat', age: 2}]
        }
    ]

我的batchwriteitem循环适用于单个项目

location: { S : item.location },

但当我尝试将pets数组作为数组输入时失败了

pets: { M:  item.pets },

所以除了pets数组外,其他都可以。

const request = pets.map(item => {
        const createdDate = Date.now();
        return {
            PutRequest: {
                Item: {   
                  location: { S : item.location },
                  createdDate:{ N: createdDate },
                  pets: { M:  item.pets }
               }
            }
        });

我是否需要将pets数组分解为类型?如果需要,我该如何实现?
编辑

pets: { L:  item.pets }, does not work

**无法读取未定义的属性(阅读“0”)

并且没有类型的旧语法在v3上对于我的文档客户机不起作用。

i7uq4tfw

i7uq4tfw1#

Pets是一个数组,也称为列表类型。您将其设置为字典/Map类型,因为您已经将值设置为M。它应该是L

pets: { L:  item.pets },

我建议您使用Document Client,因为这意味着您不需要考虑类型转换,只需使用原生JSON对象即可:

pets: item.pets,

相关问题