Tayyab
Tayyab

Reputation: 349

send multiple objects in Lambda function

I am writing a method in AWS Lambda using node.js and I am sending single object

how can I pass multiple objects?

here is my code

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"
            });
          }
    });
  });
};

and here is my object which is am sending

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

But I want to send like this in an array

[
{
  "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"
}
]

How I can send it like this. If anyone of you knows help me. Thanks

Upvotes: 0

Views: 649

Answers (1)

Sai
Sai

Reputation: 1700

If you want to send multiple objects in one go then you can send them as a array of objects like this:

[
{
  "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"
}
]

Now in your code instead of directly using the event, you need to parse it to get the array:

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

now you need to perform your desired task for each object in data by looping through it

Upvotes: 2

Related Questions