简体   繁体   中英

Using Lambda with NodeJS and fetching value correctly from Dynamo db, I cannot process the json data recieved

//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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM