I'm using
array.forEach()
parsedBody.forEach is not a function.
parseBody
forEach
forEach
console.log('- before request.post');
request.post({
url: `${dc_link}/audit`, //includes dc_IP, dc_port and /audit
headers:
{
"jwt": jwt.sign(token, jwtSecret)
}
}, function(error,response,body){ //*
console.log('path 0');
console.log('error:', error);
let parseBody = JSON.parse(body);
console.log('parseBody.additionalData', parseBody.additionalData);
parseBody.forEach(function(element){ //**HERE
console.log('path 3');
});
}
[stdout] - before request.post
[stdout] start respondWithJSON
[stderr] /app/server.js:435
[stderr] parseBody.forEach(function(element, index, array){ //*
[stderr] ^
[stderr] TypeError: parseBody.forEach is not a function
[stderr] at Request._callback (/app/server.js:435:15)
[stderr] at Request.self.callback (/app/node_modules/request/request.js:188:22)
[stderr] at emitTwo (events.js:106:13)
[stderr] at Request.emit (events.js:191:7)
[stderr] at Request.<anonymous> (/app/node_modules/request/request.js:1171:10)
[stderr] at emitOne (events.js:96:13)
[stderr] at Request.emit (events.js:188:7)
[stderr] at IncomingMessage.<anonymous> (/app/node_modules/request/request.js:1091:12)
[stderr] at IncomingMessage.g (events.js:286:16)
[stderr] at emitNone (events.js:91:20)
[stdout] path 0
[stdout] error: null
[stdout] parseBody.additionalData iAAGtYgBAGkCBFoW4F4DARkDAQkDAQUDAQ
parseBody appears to be an object since you're accessing it with dot notation. You need an array to use the forEach method. Turn parseBody into an array, and the problem is solved.
To turn it into an array, here is one possible solution:
var arr = Object.keys(parseBody).map(function(key){ return parseBody[key]; });