无法从Yelp fusion api打印商业信息

时间:2017-03-21 22:42:13

标签: node.js yelp

我正在使用yelp fusion api在某个位置搜索某种业务。我可以正确打印整个身体,但当我尝试仅记录业务或业务的特定参数时,我得到了未定义。

例如,这会打印商家:

    request(options, function (error, response, body) {
        if (error) throw new Error(error);
        console.log(body);
        res.render('index');
    });

我得到一个看起来像这样的大对象:

{"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": ""}

但是,当我尝试console.log(body.businesses)console.log(body.businesses[0].name)时,两者都未定义。是什么给了什么?

1 个答案:

答案 0 :(得分:1)

我会猜测身体"对象"实际上是一个字符串。尝试添加

console.log("Type:", typeof body)

如果它说"输入:string"试试

const bodyObj = JSON.parse(body);
console.log(bodyObj.businesses);