Javascript Nodejs请求 - 无法读取未定义的属性“forEach”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38168386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:08:31  来源:igfitidea点击:

Nodejs request - Cannot read property 'forEach' of undefined

javascriptnode.jsdrupalrequest

提问by Stuart Brown

I'm following a headless Drupal tutorial at http://blog.openlucius.com/en/blog/headless-drupal-nodejs-part-33-express-js-and-drupal-api-integrationbut my experience is with the Drupal side rather than the Node side so now that something is broken I'm not sure how to fix.

我正在关注http://blog.openlucius.com/en/blog/headless-drupal-nodejs-part-33-express-js-and-drupal-api-integration 上的无头 Drupal 教程,但我的经验是Drupal 端而不是 Node 端,所以现在有些东西坏了,我不知道如何修复。

When starting the app I get an error:

启动应用程序时出现错误:

/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)

my hellotest.js looks like:

我的 hellotest.js 看起来像:

var express = require('express');
var app = express();
var routes = require('./routes/index');
var request = require('request');
var blogsurlall = "http://www.pickingorganic.org/blogsexportall";

app.set('view engine','ejs');

var server = app.listen (2000, function(){
   console.log('Waiting for you on port 2000');
});

request({
  url:blogsurlall,
  json:true
}, function(error, response, body){
  if (!error && response.statusCode===200) {
      blogsdata_all = body;
  }

  var blogs = [];

blogsdata_all.blogs.forEach(function(item){
    blogs = blogs.concat(item);
});

  app.locals.blogsdata = blogs;
})

app.use('/', routes);

I'm assuming this is something to do with the node request not fetching the json from the 'source' site correctly which is why blogs var is empty. Not sure what is going wrong tho - any help much appreciated!

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

回答by Francois

You should console.log the response body to see how it looks like. Is it empty ?

您应该 console.log 响应正文以查看它的外观。是空的吗?

As far as I see, the url is correct and the response is :

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

[{"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":""}]

If so, you should do your forEach this way :

如果是这样,你应该这样做你的 forEach :

blogsdata_all.forEach(function(item){
    blogs.push(item);
});

because the response body is an Array of objects. The way you are trying to loop through it would be correct if the response would be like this :

因为响应体是一个对象数组。如果响应是这样的,您尝试循环的方式将是正确的:

{"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":""}] }

Also, you don't have to loop through it to push back the blog posts into a new Array, you can directly use :

此外,您不必循环遍历它以将博客文章推回到新的数组中,您可以直接使用:

app.locals.blogsdata = blogsdata_all;