Home > Software engineering >  Using Lambda with NodeJS and fetching value correctly from Dynamo db, I cannot process the json data
Using Lambda with NodeJS and fetching value correctly from Dynamo db, I cannot process the json data

Time:11-24

//Function to pull data from Dynamo DB (works)

async function pullone(sessionid) {
  const params = {
    TableName: dynamodbTableName,
    Key: {
      'sessionid': sessionid
    }
  };
  return await dynamodb.get(params).promise().then((response) => {
    return response.Item
  }, (error) => {
    console.error('Do your custom error handling here. I am just gonna log it: ', error);
  });
}

//executing it

`exports.handler = async (event) => {
  
let sessionid = "45883754"
let data = pullone(sessionid)
return data

};`

//The above works well and the 'data' returned is

{   "school": "3wc",   "sessionid": "45883754" }

I have tried to get values with data.school but itdoes not work. Is this an issue with Lambda because this is supposed to be a nobrainer. Your support will be appreciated. Thanks guys

I tried data.school or data.sessionid but the values were not coming forth

CodePudding user response:

For future posts, please show the error message or what you recieved that was unexpected. Im your case, the Lambda function is doing an asynchronous call to fetch the data meaning that the lambda itself is asynchronous. In your code that calls the Lambda, you meed to add await such that the call looks like this:

let data = await pullone(sessionid)
console.log(data.school)
  • Related