将对象数组插入 DynamoDB

Insert array of Objects into DynamoDB

我花了几个小时试图弄清楚如何将对象数组插入到 DynamoDB 中,但找不到明确的答案。玩了一会儿之后,我找到了解决办法。见下文。仅供参考,我将其包装在一个承诺中,但我没有将其包含在代码中。我希望这对某人有所帮助。

    const AWS = require("aws-sdk");    

    AWS.config.update({
      region: <<Your Regions>>,
      endpoint: <<Your endpoint>>
    });

    var dynamo = new AWS.DynamoDB();

    var params = {
       TableName: <<name of your table>>,
       Item: {
           'PK' : {"S": `<<Your ITEM'S PK>>`},
           'SK' : {"S": `<<YOUR ITEM'S SK`},
           'RecentLocations': {"L": [ //>>>>>>>>>>>>>> ARRAY OF OBJECTS STARTS HERE!!!!
               { //we start the first value here
                   "M": { //we specify it is a map/object
                       "path": {"S": device.oneLocationOnly.path }, //FIRST PROPERTY AND VALUE
                       "locationArray": { //SECOND PROPERTY, WHICH IS AN ARRAY/LIST
                           "L": device.recentLocations.locationArray
                       }
                   }
               }
           ]}
       },
           ConditionExpression: "attribute_not_exists(PK)"
   };

dynamo.putItem(params, function(err, data) {
  if (err) {
      console.log("Error", err);
      resolve([false, err])
  } else {
      resolve([true]);
  }
});