0

Node.js request - Fetching response values not working

I'm using node.js and request but I'm not able to fetch the desired values 'name'. What am I missing in my setup?

request.get({
  url: "http://127.0.0.1:4004/api/search?name=peter"
}, (error, response, body) => {
    if(error) {
      console.log(error);
      return console.dir(error);
    } else {
      console.log(response.body);
      var getBody = JSON.parse(response.body);
      if (getBody.status == 200) {
        console.log('Response: '+getBody);
        //console.log('Records: '+getBody.records.name[0]);
        console.log('Records: '+getBody.records.name);
        res.render('profile', {
          page:'Profile',
          menuId:'profile',
          data: {},
          errors: {}
        });
      } 
    }
});

Console.log:

{"status":200,"error":null,"records":[{"id":235,"abc":"235980JFD","name":"Peter Pan"}]}
Response: [object Object]
Records: undefined

1 Answer 1

1

records is an array so you have to use:

console.log('Records: '+getBody.records[0].name);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.