I'm following a headless Drupal tutorial at http://blog.openlucius.com/en/blog/headless-drupal-nodejs-part-33-express-js-and-drupal-api-integration but 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.
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:
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!