簡體   English   中英

Nodejs請求 - 無法讀取未定義的屬性'forEach'

[英]Nodejs request - Cannot read property 'forEach' of undefined

我正在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);

我假設這與節點請求沒有正確地從'source'站點獲取json有關,這就是blogs var為空的原因。 不知道出了什么問題 - 任何幫助都非常感謝!

您應該在console.log中查看響應正文,看看它是什么樣的。 它是空的嗎?

據我所知,網址是正確的,響應是:

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

如果是這樣,你應該這樣做你的forEach:

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; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM