gpt4 book ai didi

javascript - Nodejs 请求 - 无法读取未定义的属性 'forEach'

转载 作者:行者123 更新时间:2023-11-30 11:55:22 25 4
gpt4 key购买 nike

我正在学习 http://blog.openlucius.com/en/blog/headless-drupal-nodejs-part-33-express-js-and-drupal-api-integration 上的 headless 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);

我假设这与 Node 请求没有从“源”站点正确获取 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":""}]

如果是这样,你应该这样做你的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; 

关于javascript - Nodejs 请求 - 无法读取未定义的属性 'forEach',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38168386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com