簡體   English   中英

Express.js API調用使我的服務器崩潰

[英]Express.js API call is crashing my server

我是express.js和API調用的新手,但我不知道為什么這會導致服務器崩潰? 本質上,它將第一次運行並呈現頁面,但是隨后使服務器崩潰,並顯示:

TypeError: Cannot read property 'length' of undefined

for (var i = 0; i < data.businesses.length; i++) {

代碼的相關部分:

router.get('/:term/:radius/:lat/:lng', function (req, res) {

var yelp = require("yelp").createClient({
    consumer_key: "xxxx", 
    consumer_secret: "xxxx",
    token: "xxxx",
    token_secret: "xxxx"
});

yelp.search({
    term: req.params.term,
    radius_filter: req.params.radius,
    ll: req.params.lat + ',' + req.params.lng 
},
function (error, data) {

    var businessesArr = [];
    if (typeof data) {
        for (var i = 0; i < data.businesses.length; i++) {
            businessesArr.push({
                name: data.businesses[i].name,
                image_url: data.businesses[i].image_url
            });
        }
        res.render('selection', {
            businesses: businessesArr
        });
            // console.log(data);
    } else {
        console.log(error);
    }
});

});

這行:

 if (typeof data) {

總是要求真,因為無論如何它實際上都會返回一個字符串。

替換為:

   if (data && data.businesses) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM