【问题标题】:Adding multiple Maps to a List using DynamoDb, Lambda, Api Gateway使用 DynamoDb、Lambda、Api 网关将多个地图添加到列表
【发布时间】:2018-11-27 18:22:28
【问题描述】:

我正在向 Api Gateway 传递一个参数,以使用 AWS Lambda Node JS 将其保存在 DynamoDB 中。

Api Gateway 的集成请求是:

#set($inputRoot = $input.path('$'))
{
  "name" : "$inputRoot.name",
  "profileImage": "$inputRoot.profileImage",
  "gender" : "$inputRoot.gender",
  "interests": #foreach ( $item in ["$inputRoot.interests"] ) $item #end,
  "surveyOne": "$inputRoot.surveyOne",
  "surveyOneAnswer": "$inputRoot.surveyOneAnswer",
  "surveyTwo": "$inputRoot.surveyTwo",
  "surveyTwoAnswer": "$inputRoot.surveyTwoAnswer"
}

AWS Lambda(Node JS)的内容接收参数并保存到DynamoDB:

const params = {
    Item: {
      'uuid': { S: "i_" + uuidv4() }, 
      'name': { S: event.name }, 
      'profileImage': { S: event.profileImage },
      'gender': { S: event.gender },
      'interests': { SS: event.interests },
      'surveys' : {
        L: [
          { 
            M: {
              'type': { S: event.surveyOne },
              'answer': { S: event.surveyOneAnswer },
            },
            M: {
              'type': { S: event.surveyTwo },
              'answer': { S: event.surveyTwoAnswer }
            }
          }
        ]
      }
     }, 
     TableName: 'users'
   };

   dynamodb.putItem(params, (err, data) => {
     if (err) {
      const response = {
        statusCode: 500, 
        headers: {
          'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({status: false})
      };
      return callback(null, response);  
    } 

    // return status 200 if success
    const response = {
      statusCode: 200, 
      headers: {
        'Access-Control-Allow-Origin': '*'
      },
      body: JSON.stringify({status: 'A new record has been added'})
    };
    return callback(null, response);

   })

但 Dynamo DB 中保存的项目仅包含 一项调查,即 surveyTwo。应该是 2 因为我通过了surveyOne 和surveyTwo 输入和回答。

我的预期结果应该是

{
  "name": "John Doe",
  "profileImage": "https://example.com", 
  "gender": "m", 
  "interests": ["Basketball",  "Swimming"],
  "surveys": [
    { "type": "question 1", "answer": "answer to question 1" },
    { "type": "question 2", "answer": "answer to question 2" }
  ]
}

【问题讨论】:

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


    【解决方案1】:

    我建议使用DynamoDb DocumentClient 类。

    您可以提供相同的参数,但使用原生 JS 类型。

    DocumentClient 为您进行编组和解组,这可能是您的surveyTwo 问题的问题。

    您的示例将如下所示。

    var params = {
     TableName : 'users',
     Item: {
         uuid: 'i_' + uuidv4(),
         name: event.name,
         surveys: [
            {type: event.surveyOne, answer: event.surveyOneAnswer},
            {type: event.surveyTwo, answer: event.surveyTwoAnswer}]
         }
    };
    
    var documentClient = new AWS.DynamoDB.DocumentClient();
    
      documentClient.put(params, function(err, data) {
     if (err) console.log(err);
     else console.log(data);
    });
    

    【讨论】:

    • 是的,我也使用 Document Client 修复了该错误。谢谢你的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2017-09-05
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    相关资源
    最近更新 更多