2016-08-03 91 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