skip to Main Content

I’m calling my Lambda from an API Gateway and as Input give it a userID and a MMMY, which is just Month and year.
Besides that I input data in the form of a List of Objects that look like this:

{
  "data": [
    {
      "dt": 3,
      "bl": 0,
      "bs": "Einnahme Fahrten",
      "ss": 7
    },
    {
      "dt": 28,
      "bl": 0,
      "bs": "Einnahme Fahrten",
      "ss": 7
    },//can grow a lot more
  ]
}

How can I adjust my code that is just inputting the first item of the List, to store the whole list:

switch (event.routeKey) {
      case "PUT /sheets/{userID}/{MMYY}":
        console.log(
          "MYY",
          event.pathParameters.MMYY,
          "userID",
          event.pathParameters.userID,
          "Json",
          JSON.parse(event.body)
        );
        // Assuming the request body is in the form { "data": [...] }
        const data = event.body ? JSON.parse(event.body).data : [];
        const MMYY = parseInt(event.pathParameters.MMYY);
        // Create an array of PutCommand operations
        const putCommands = data.map((item) => {
          return new PutCommand({
            TableName: tableName,
            Item: {
              userID: event.pathParameters.userID,
              MMYY: MMYY,
              dt: item.dt,
              bl: item.bl,
              bs: item.bs,
              ss: item.ss,
            },
          });
        });

        // Execute all PutCommand operations
        await Promise.all(putCommands.map((command) => dynamo.send(command)));

2

Answers


  1. Chosen as BEST ANSWER

    Okay i solved it by doing this, is this a good answer?

    switch (event.routeKey) {
              case "PUT /sheets/{userID}/{MMYY}":
                const MMYY = parseInt(event.pathParameters.MMYY);
        
                // Assuming the request body is in the form { "data": [...] }
                const data = event.body ? JSON.parse(event.body).data : [];
        
                // Create an array of PutCommand operations
                const putCommands = [
                  new PutCommand({
                    TableName: tableName,
                    Item: {
                      userID: event.pathParameters.userID,
                      MMYY: MMYY,
                      data: data.map((item) => ({
                        dt: item.dt,
                        bl: item.bl,
                        bs: item.bs,
                        ss: item.ss,
                      })),
                    },
                  }),
                ];
        
                // Execute all PutCommand operations
                await Promise.all(putCommands.map((command) => dynamo.send(command)));
    

  2. It should be like this:

    switch (event.routeKey) {
              case "PUT /sheets/{userID}/{MMYY}":
                const MMYY = parseInt(event.pathParameters.MMYY);
        
                // Assuming the request body is in the form { "data": [...] }
                const data = event.body ? JSON.parse(event.body).data : [];
        
                // Create an array of PutCommand operations
                  new PutCommand({
                    TableName: tableName,
                    Item: {
                      userID: event.pathParameters.userID,
                      MMYY: MMYY,
                      data: data[0]
                    },
                  })
        
                // Execute all PutCommand operations
                await dynamo.send(command);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search