【问题标题】:Can't print business information from Yelp fusion api无法从 Yelp fusion api 打印业务信息
【发布时间】:2017-03-22 08:53:46
【问题描述】:

我正在使用 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) 时,两者都未定义。什么给了?

【问题讨论】:

    标签: node.js yelp


    【解决方案1】:

    我猜body“object”实际上是一个字符串。尝试添加

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

    如果它显示“类型:字符串”,请尝试

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

    【讨论】:

    • 这确实是问题所在。哦!谢谢!
    猜你喜欢
    • 2021-10-28
    • 2023-03-20
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多