Javascript Nodejs请求-无法读取属性';forEach&x27;未定义的

Javascript Nodejs请求-无法读取属性';forEach&x27;未定义的,javascript,node.js,drupal,request,Javascript,Node.js,Drupal,Request,我在学习一个无头Drupal教程,但我的经验是使用Drupal端而不是节点端,所以现在有些东西坏了,我不知道如何修复 启动应用程序时,我收到一个错误: /Library/WebServer/Documents/uno-fe/hellotest.js:23 blogsdata_all.blogs.forEach(function(item){ ^ TypeError: Cannot read property 'forEach' of undefined

我在学习一个无头Drupal教程,但我的经验是使用Drupal端而不是节点端,所以现在有些东西坏了,我不知道如何修复

启动应用程序时,我收到一个错误:

/Library/WebServer/Documents/uno-fe/hellotest.js:23
blogsdata_all.blogs.forEach(function(item){
                   ^

TypeError: Cannot read property 'forEach' of undefined
    at Request._callback (/Library/WebServer/Documents/uno-fe/hellotest.js:23:24)
    at Request.self.callback (/Library/WebServer/Documents/uno-fe/node_modules/request/request.js:200:22)
    at emitTwo (events.js:87:13)
    at Request.emit (events.js:172:7)
    at Request.<anonymous> (/Library/WebServer/Documents/uno-fe/node_modules/request/request.js:1067:10)
    at emitOne (events.js:82:20)
    at Request.emit (events.js:169:7)
    at IncomingMessage.<anonymous> (/Library/WebServer/Documents/uno-fe/node_modules/request/request.js:988:12)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)

我假设这与节点请求没有从“源”站点正确获取json有关,这就是blogs var为空的原因。不知道出了什么问题-非常感谢任何帮助

您应该对响应主体进行console.log记录,以查看其外观。是空的吗

据我所知,url是正确的,响应是:

[{"title":"second post","created":"2016-06-30T15:49:17+08:00","field_blog_image":"  \n\n","path":"\/node\/2","created_1":"30 2016 Jun","body_1":"some content","body":"some content","uid":"","user_picture":""},{"title":"first post","created":"2016-06-28T21:56:52+08:00","field_blog_image":"  \n\n","path":"\/node\/1","created_1":"28 2016 Jun","body_1":"woot","body":"woot","uid":"","user_picture":""}]
如果是这样,你应该这样做:

blogsdata_all.forEach(function(item){
    blogs.push(item);
});
因为响应主体是一个对象数组。如果响应如下所示,则您尝试循环的方式是正确的:

{"blogs":[{"title":"second post","created":"2016-06-30T15:49:17+08:00","field_blog_image":"  \n\n","path":"\/node\/2","created_1":"30 2016 Jun","body_1":"some content","body":"some content","uid":"","user_picture":""},{"title":"first post","created":"2016-06-28T21:56:52+08:00","field_blog_image":"  \n\n","path":"\/node\/1","created_1":"28 2016 Jun","body_1":"woot","body":"woot","uid":"","user_picture":""}] }
此外,您不必通过循环将博客文章推回到一个新的数组中,您可以直接使用:

app.locals.blogsdata = blogsdata_all; 

检查
错误
和响应代码。当然,获取dataThnx时存在问题。这对我很有帮助,对于我的项目,我用
年龄组来解决这个问题:any=[]
app.locals.blogsdata = blogsdata_all;