yoges nsamy
yoges nsamy

Reputation: 1435

Sending pathParameters correctly to AWS Lambda NodeJS function - calling from AWS console works but not from browser/Postman

I have a NodeJS lambda function that works when tested from the AWS console, but when called from the browser or Postman returns "Cannot read property 'id' of undefined".

This is the value I use as test event when testing the function from the AWS console:

{
  "pathParameters": {
    "id": "gamuda"
  }
}

I added console.log to see the value of 'event' as suggested on my other post and notice that the value is not being read when called from browser/postman.

2020-06-22T12:17:24.096Z 37f76d74-ef02-401e-89a6-c00eedcf56b3 INFO {} 

How can I read the value correctly? Here's my code: const id = event.pathParameters.id;

module.exports.exportContactsByClientID = (event, context, callback) => {
  console.log(event);
  const id = event.pathParameters.id;
  // const id = 'mkh';
  
  const params = {
    TableName: contactsTable,
    IndexName: "client_code-created_at_local-index",
    KeyConditionExpression: "client_code = :v_ID",
    ExpressionAttributeValues: {
      ":v_ID": id,
    },
    ProjectionExpression: "client_code, created_at, contact_name, age, phone, email, dsr_ratio, max_eligibility, selected_banks"
  };

return db.query(params)
.promise()
.then((res) => {
  // console.log(res);
  const items = res.Items;
  // Headers + Columns
  worksheet.cell(1, 1).string('Client').style({font: {bold: true}});
  worksheet.cell(1, 2).string('Name').style({font: {bold: true}});
  worksheet.cell(1, 3).string('Age').style({font: {bold: true}});
  worksheet.cell(1, 4).string('Email').style({font: {bold: true}});
  worksheet.cell(1, 5).string('Phone').style({font: {bold: true}});
  worksheet.cell(1, 6).string('DSR Ratio').style({font: {bold: true}});
  worksheet.cell(1, 7).string('Max Eligibility').style({font: {bold: true}});
  // Rows
  // items.sort((a, b) => (a.date > b.date) ? -1 : 1);
  items.forEach((item, i) => {
    console.log(item.contact_name);
    worksheet.cell(i + 2, 1).string(item.client_code);
    worksheet.cell(i + 2, 2).string(item.contact_name);
    worksheet.cell(i + 2, 3).string(item.age);
    worksheet.cell(i + 2, 4).string(item.email);
    worksheet.cell(i + 2, 5).string(item.phone);
    worksheet.cell(i + 2, 6).string(item.dsr_ratio);
    worksheet.cell(i + 2, 7).string(item.max_eligibility);
  });
  console.log(contactsBucket);
  console.log(id);
  return workbook.writeToBuffer().then(buffer => {
    console.log('inside writetoubgger')
    var params = {
      Bucket: contactsBucket,
      Key: `contacts/${id}.xlsx`,
      Body: buffer,
      ACL: 'public-read'
    }
    // S3.upload(params, function(err, data) {
    //   if (err) {
    //     console.log(err, err.stack);
    //   } else {
    //     // callback(null, response(200, res));
    //     callback(null, response(200, res));
    //   }
    // });
    return S3.upload(params).promise().then(data => {
      callback(null, response(200, res));
});
  })
})
.catch((err) => callback(null, response(err.statusCode, err)));
};

I use similar approach in reading id when getting contacts by id and this function works fine:

module.exports.getContactsByClientID = (event, context, callback) => {
  const id = event.pathParameters.id.toLowerCase();
  const newDt = new Date();
  const filterDt = new Date(newDt.getTime() - (newDt.getTimezoneOffset() * 60000 )).toISOString().split("T")[0];

  var params = {
    TableName: contactsTable,
    IndexName: "client_code-created_at_local-index",
    KeyConditionExpression: "client_code = :v_ID AND created_at_local = :v_created",
    ExpressionAttributeValues: {
      ":v_ID": id,
      ":v_created": filterDt
    },
    ProjectionExpression: "client_code, contact_name, age, phone, email, dsr_ratio, max_eligibility, selected_banks"
  };

return db.query(params)
.promise()
.then((res) => {
  console.log(res);
  callback(null, response(200, res));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};

serverless.yml:

service: mhub-dsr-calculator-api

custom:
  allowed-headers:
      - Content-Type
      - Authorization
      - X-Api-Token
      - X-Origin
      - X-Amz-Date
      - X-Amz-Security-Token
  settings:
    CONTACTS_TABLE: dsrcalculator-contacts-dev
    CONTACTS_BUCKET: mhub-dsrcalculator-contacts

provider:
  name: aws
  runtime: nodejs12.x
  environment: ${self:custom.settings}
  region: ap-southeast-1 
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
        - dynamodb:Query
      Resource:
        - "arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.settings.CONTACTS_TABLE}"
    - Effect: "Allow"
      Action:
        - dynamodb:Query
      Resource:
        - "arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.settings.CONTACTS_TABLE}/index/client_code-created_at_local-index"
    - Effect: "Allow"
      Action:
       - "s3:*"
      Resource: 
       - "arn:aws:s3:::${self:custom.settings.CONTACTS_BUCKET}/*"

functions:
  createPost:
    handler: handler.createContact
    events:
    - http:
        path: /contact
        method: post
        cors: true
  updatePost:
    handler: handler.updateContact
    events:
    - http:
        path: /contact/{id}
        method: put
        cors: true
  getAllPosts:
    handler: handler.getAllContacts
    events:
    - http:
        path: /contacts
        method: get
        cors: true
  getContactsByClientID:
    handler: handler.getContactsByClientID
    events:
    - http:
        path: /contacts/{id}
        method: get
        cors: true
  exportContactsByClientID:
    handler: handler.exportContactsByClientID
    events:
    - http:
        path: /export/{id}
        method: get
        cors: true

resources:
  Resources:
    PostsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
        - AttributeName: "id"
          AttributeType: "S"
        - AttributeName: "client_code"
          AttributeType: "S"
        - AttributeName: "created_at_local"
          AttributeType: "S"
        KeySchema:
        - AttributeName: "id"
          KeyType: "HASH"
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:custom.settings.CONTACTS_TABLE}
    

I've been at it the whole day and probably missed something silly :( Any tips and guidance will be appreciated.

Upvotes: 0

Views: 1385

Answers (1)

yoges nsamy
yoges nsamy

Reputation: 1435

My issue was related to the setting on the API Gateway. Previously export/{id} had the option 'Use Lambda Proxy integration' unchecked (as I was testing something).

As the guide say, this needs to be checked so that our requests details will be available on the event.

One question remains - why didn't my test on the AWS console fail.

enter image description here

Upvotes: 2

Related Questions