I am using the yelp fusion api to search for a type of business in a location. I can print the whole body correctly but when I try to log just the businesses or a specific parameter of a business I get undefined.
For example, this prints the businesses:
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
res.render('index');
});
I get a large object which looks something like this:
{"total": 106, "businesses": [{"transactions": [], "phone": "+15409512483", "name": "The Rivermill", "display_phone": "(540) 951-2483", "price": "$", "review_count": 63, "rating": 4.0, "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/mQbuIZ9uRsXMwIW9UJiHsQ/o.jpg", "id": "the-rivermill-blacksburg", "distance": 511.45787585319994, "location": {"display_address": ["212 Draper Rd", "Blacksburg, VA 24060"], "city": "Blacksburg", "country": "US", "zip_code": "24060", "address1": "212 Draper Rd", "state": "VA", "address2": "", "address3": ""}
However, when I try console.log(body.businesses)
or console.log(body.businesses[0].name)
both undefined. What gives?
I'm going to guess that the body "object" is actually a string. Try adding
console.log("Type:", typeof body)
and if it says "Type: string" try
const bodyObj = JSON.parse(body);
console.log(bodyObj.businesses);