【问题标题】:Nodejs request - Cannot read property 'forEach' of undefinedNodejs请求 - 无法读取未定义的属性'forEach'
【发布时间】:2016-07-03 09:02:34
【问题描述】:

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

启动应用时出现错误:

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

我的 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);

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

【问题讨论】:

  • 检查error 和响应代码。取数据当然有问题

标签: javascript node.js drupal request


【解决方案1】:

您应该控制台记录响应正文以查看其外观。是空的吗?

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

[{"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; 

【讨论】:

  • Thnx 。这对我有帮助,对于我的项目,我用Age_groups: any = []; 解决了这个问题
猜你喜欢
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 2021-08-23
  • 2021-12-10
  • 2019-09-16
  • 1970-01-01
相关资源
最近更新 更多