0

I need to get only the user first and second names from a response body. I am trying to get only these from the response by assigning them to a variable e.g let usersFirstName = response.body[0].first_name. However, for some reason the response body is being treated as a String and when I try the above, there is no such thing as first_name. I have also tried doing response.body[0][0] and as its a string it just gets the first '{' in the response.body - again not treating it as an object but a string. Why is this? Is this normal and how do I get just the user names from the response?

var express = require('express');
var router = express.Router();
var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://bpdts-test-app.herokuapp.com/city/London/users',
  'headers': {
    'accept': 'application/json'
  }
};

request(options, function (error, response) {

  if (error) throw new Error(error);
  //console.log(response.body);
  let userNames = '';

  let obj = response.body;

  // for (let i = 0; i<obj.length; i++){
  //   userNames = obj[0];
  //   console.log(userNames)
  // }
  console.log(obj);
  //console.log(userNames);
  //console.log(response.body[0].id);
  return JSON.stringify(response.body);
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: request(options) });
  //console.log(request(options));
});

module.exports = router;

The return of the request response's body:

   [{"id": 135, "first_name": "Mechelle", "last_name": "Boam", "email": "mboam3q@thetimes.co.uk", "ip_address": "113.71.242.187", "latitude": -6.5115909, "longitude": 105.652983}, {"id": 396, "first_name": "Terry", "last_name": "Stowgill", "email": "tstowgillaz@webeden.co.uk", "ip_address": "143.190.50.240", "latitude": -6.7098551, "longitude": 111.3479498}, {"id": 520, "first_name": "Andrew", "last_name": "Seabrocke", "email": "aseabrockeef@indiegogo.com", "ip_address": "28.146.197.176", "latitude": "27.69417", "longitude": "109.73583"}, {"id": 658, "first_name": "Stephen", "last_name": "Mapstone", "email": "smapstonei9@bandcamp.com", "ip_address": "187.79.141.124", "latitude": -8.1844859, "longitude": 113.6680747}, {"id": 688, "first_name": "Tiffi", "last_name": "Colbertson", "email": "tcolbertsonj3@vimeo.com", "ip_address": "141.49.93.0", "latitude": 37.13, "longitude": -84.08}, {"id": 794, "first_name": "Katee", "last_name": "Gopsall", "email": "kgopsallm1@cam.ac.uk", "ip_address": "203.138.133.164", "latitude": 5.7204203, "longitude": 10.901604}]

Even stranger is that its being treated as an object n the locally hosted web page: locally hosted webpage

extends layout

block content
  p Below are all the users that live in London #{title}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Marcooz2007
  • 335
  • 1
  • 9
  • The line `res.render('index', { title: request(options) });` sets `title` to the output of `request(options)`, which is *not* the return value of callback. – Heretic Monkey Jun 01 '20 at 13:15
  • Thanks for your help! Could you let me know how II would get the value of the callback? I thought my function returns the response body as a string – Marcooz2007 Jun 01 '20 at 13:21
  • So, AFAIK, the `request` npm package has been deprecated, so you should be thinking about using something like node-fetch or axios (or just plain old `http.request`) to get data from the API. Then you should read the answers to [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/215552). – Heretic Monkey Jun 01 '20 at 13:25
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Heretic Monkey Jun 01 '20 at 13:32

0 Answers0