I am trying to do a GET method inside Nodejs. After doing the GET, I want to use the MAP function and collect all the names as an array.
But I am getting the following error,
/root/server.js:21
var op = body.products.map(function(item) {
^
TypeError: Cannot read property 'map' of undefined
at IncomingMessage.<anonymous> (/root/server.js:21:27)
at emitNone (events.js:85:20)
at IncomingMessage.emit (events.js:179:7)
at endReadableNT (_stream_readable.js:913:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
CODE is,
var http = require('http');
console.log("hi")
var options = {
host: 'api.usergrid.com',
path: '/siddharth1/sandbox/restaurants'
};
var req = http.get(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
// Buffer the body entirely for processing as a whole.
var bodyChunks = [];
res.on('data', function(chunk) {
// You can process streamed parts here...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
console.log('BODY: ' + body);
// ...and/or process the entire body here.
var op = input.entities.map(function(item) {
return item.name;
});
console.log(op);
})
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});
console.log('Server listening on port 80');
I do not know where I am going wrong, because in console I can see the body having json data.
Kidnly help me out.