отправить несколько объектов в лямбда-функцию - PullRequest
0 голосов
/ 14 июля 2020

Я пишу метод в AWS Lambda, используя node.js, и я отправляю один объект

как я могу передать несколько объектов?

вот мой код

const mysql = require('mysql');
var pool  = mysql.createPool({ //  a connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required
   host: process.env.RDS_HOSTNAME,
   user: process.env.RDS_USERNAME,
   password: process.env.RDS_PASSWORD,
   port: process.env.RDS_PORT,
  database: process.env.RDS_DATABASE
});

exports.handler = (event, context, callback) => {
    // console.log(event);
  let CategoryId = event['CategoryId'];
  let Name = event['Name'];
  let Description = event['Description'];
  let UpdatedBy = event['UpdatedBy'];
  let WrapupId = event['WrapupId'];
  // allows for using callbacks as finish/error-handlers
 context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
    if (err) throw err;
    let sql =`UPDATE ctrData2.WrapupCodes SET CategoryId=?, Name=?, Description=?, UpdatedBy=?, UpdatedDate= now() WHERE WrapupId=?`;
     let field = [CategoryId, Name, Description, UpdatedBy, WrapupId];    
    connection.query(sql,field, function (err, result, fields) {
      if (err) throw err;
      connection.release();
      console.log(result);
      console.log(typeof(result.affectedRows));
      let affectedRows = result.affectedRows;
      if(result && affectedRows > 0){
      let response ={
            status:200,
            message:"Data has been updated successfully"
            //  Name : field[0],
            //  Description : field[1],
            //  UpdatedBy : field[2],
            //  UpdatedDate : field[3],
          };
        //   console.log("I am here:",response);
          callback(null,response);
      }else{
            callback(null,{
              status: 404,
              message: "id not found"
            });
          }
    });
  });
};

и вот мой объект, который я отправляю

{
  "CategoryId": "10",
  "Name": "Credit Card Service",
  "Description": "This wrapup should assign to credit card service",
  "UpdatedBy": "Elys Root",
  "WrapupId": "22"
}

Но я хочу отправить вот так в массиве

[
{
  "CategoryId": "10",
  "Name": "Credit Card Service",
  "Description": "This wrapup should assign to credit card service",
  "UpdatedBy": "Elys Root",
  "WrapupId": "22"
}
],

[
{
  "CategoryId": "10",
  "Name": "Credit Card Service",
  "Description": "This wrapup should assign to credit card service",
  "UpdatedBy": "Elys Root",
  "WrapupId": "21"
}
]

Как я могу отправить его вот так. Если кто-нибудь из вас знает, помогите мне. Спасибо

1 Ответ

1 голос
/ 14 июля 2020

Если вы хотите отправить несколько объектов в одном go, вы можете отправить их в виде массива объектов следующим образом:

[
{
  "CategoryId": "10",
  "Name": "Credit Card Service",
  "Description": "This wrapup should assign to credit card service",
  "UpdatedBy": "Elys Root",
  "WrapupId": "22"
},
{
  "CategoryId": "10",
  "Name": "Credit Card Service",
  "Description": "This wrapup should assign to credit card service",
  "UpdatedBy": "Elys Root",
  "WrapupId": "21"
}
]

Теперь в вашем коде вместо прямого использования события вам нужно чтобы проанализировать его и получить массив:

// data is array of objects
let data = JSON.parse(event)

теперь вам нужно выполнить желаемую задачу для каждого объекта в данных, пройдя через него

...