如何在AWS Lambda node.js中为updateitem dynamodb创建运行时参数

问题描述 投票:0回答:1
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'ap-south-1', apiVersion: '2012-08-10'});
exports.handler = (event, context, callback) => { 

updateExpression = null;

var expressionAttributeValues = {};
if (event.father_status != undefined) { 

if (updateExpression == null) {
    updateExpression = "set father_status = :fs";
}

else {
   updateExpression += ", father_status = :fs"
     }

expressionAttributeValues.fs = {S: event.father_status};
   }

   if (event.mother_status != undefined){

if (updateExpression == null) {

updateExpression = "set mother_status = :ms";

}else {
           updateExpression += ", mother_status = :ms"
       } 
     expressionAttributeValues.ms = {S: event.mother_status};
   }

   var params = {
    TableName: "user-family-background",
    Key:{
        "user_id": {
            S: event.user_id
        }
    },
    UpdateExpression: updateExpression,
    ExpressionAttributeValues: expressionAttributeValues,
    ReturnValues:"ALL_NEW"
};

dynamodb.updateItem(params, function(err, data) {
       if(err){
           console.log("error: " + err);
           callback(err);
       }
       else {
           console.log("sucsess: " + JSON.stringify(data));
           callback(null, data);
       }
   });

   console.log(params);
   console.log(updateExpression);
   console.log(JSON.stringify(expressionAttributeValues));

};

我想为“expressionAttributeValeus”创建类似这样的JSON:

{ ':fs': { S: 'employed' }, ':ms': { S: 'homemaker' } }

但我得到这样的JSON

{ 'fs': { S: 'employed' }, 'ms': { S: 'homemaker' } }

没有(:“fs”前面的冒号和“ms”)

如果有人可以帮助,将不胜感激。

amazon-web-services aws-lambda amazon-dynamodb
1个回答
1
投票

将您的代码更改为

  expressionAttributeValues[':fs'] = {S: event.father_status};

  expressionAttributeValues[':ms'] = {S: event.mother_status};
© www.soinside.com 2019 - 2024. All rights reserved.