2016-08-03 88 views
0

我試圖在我的應用上實現ngInfiniteScroll - Loading Remote Data用AngularJS從NodeJS服務器返回JSONP

演示正常工作&我成功獲取Reddit API,但無法讓我的列表工作。

我成功點擊200服務器響應&可以在開發工具中看到我的數據。這些答案已幫助1 & 2但我仍然不完全確定該怎麼做。


EDIT(見角工廠編輯):

此回調現在的工作和$http是要成功但我現在越來越Cannot read property 'length' of undefined


角廠:

Reddit.prototype.nextPage = function() { 
    if (this.busy) return; 
    this.busy = true; 

    // Edit - I changed this var from 
    // var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&jsonp=JSON_CALLBACK"; 
    // to 
    var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&callback=JSON_CALLBACK"; 

    $http.jsonp(url) 
    .success(function(data) { 
     console.log(data); 
     var items = data.children; 
     for (var i = 0; i < items.length; i++) { 
     this.items.push(items[i].data); 
     } 
     this.after = "t3_" + this.items[this.items.length - 1].id; 
     this.busy = false; 
    }.bind(this)); 
    console.log('Reddit.prototype'); 
}; 

NodeJS Route:

app.get('/list', function (req, res) { 

    Found.find(function (err, post) { 
    if (err) { 
     res.send(err); 
    } 
    res.jsonp(post); 
    }); 

}); 
+0

項目必須是undefined否在其他地方看不到'.length'?試着在你有console.log的地方放一個'debugger'行,然後你可以在變量運行時檢查它的值。嗯,我認爲我看到它......會發佈一個答案....不要回答有關答案,但是當你做.length時,項目肯定是不確定的,否則就不會有這個錯誤。 – shaunhusain

+0

@shaunhusain,所以我的數據以'data = [Object,Object]'的形式返回,並返回所有從服務器返回的值。 – Mark

+0

嘗試交互式調試器(F12 - > Sources Panel,或者只打開F12並添加一個調試器;'代碼中的語句),您應該能夠添加監視器或檢查其中的局部變量以查看數據是什麼以及data.children具有和驗證項目是否爲數組,只需使用F10或小數點調試工具中的箭頭越過圓圈。 – shaunhusain

回答

2
Reddit.prototype.nextPage = function() { 
    if (this.busy) return; 
    this.busy = true; 

    // Edit - I changed this var from 
    // var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&jsonp=JSON_CALLBACK"; 
    // to 
    var url = config_api.API_ENDPOINT_LOCAL + "list?after=" + this.after + "?alt=json-in-script&callback=JSON_CALLBACK"; 

    $http.jsonp(url) 
    .success(function(data) { 
     console.log(data); 
     var items = data; 
     for (var i = 0; i < items.length; i++) { 
     this.items.push(items[i]); 
     } 
     this.after = "t3_" + this.items[this.items.length - 1].id; 
     this.busy = false; 
    }.bind(this)); 
    console.log('Reddit.prototype'); 
}; 

應該修復它!

+0

100%工作!謝謝! – Mark