我正在尝试在Nodejs中执行GET方法。在执行GET之后,我想使用MAP函数并将所有名称收集为数组。
但是我收到以下错误,
/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是,
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');
我不知道我哪里出错了,因为在控制台我可以看到身体有json数据。
Kidnly帮帮我。
答案 0 :(得分:0)
谢谢@Felix Kling
如你所说,我补充说,
var body2 = JSON.parse(body);我得到了阵列。
var body2 = JSON.parse(body);
// ...and/or process the entire body here.
var op = body2.entities.map(function(item) {
return item.name;
});
console.log(op);
如果我有任何其他疑问,我会回来。