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

时间:2019-04-08 18:18:57

标签: amazon-web-services aws-lambda amazon-dynamodb

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”前的冒号)

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

1 个答案:

答案 0 :(得分:1)

将代码更改为

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

  expressionAttributeValues[':ms'] = {S: event.mother_status};