I am dealing with data in the following format:
[
{ device_id: '12335',
timestamp: '2018-05-14T08:31:23.000Z',
temperature: 21,
pressure: 31,
humidity: 20,
equipment_name: 'truck5' },
{ device_id: '12335',
timestamp: '2018-05-14T08:31:31.000Z',
temperature: 28,
pressure: 35,
humidity: 25,
equipment_name: 'truck5' },
{ device_id: '12335',
timestamp: '2018-05-14T08:31:33.000Z',
temperature: 36,
pressure: 44,
humidity: 33,
equipment_name: 'truck5' },
{ device_id: '12335',
timestamp: '2018-05-14T08:31:36.000Z',
temperature: 31,
pressure: 39,
humidity: 30,
equipment_name: 'truck5' },
// additional data here...
This data is retrieved from my SQL database and stored as follows:
var res1 =[];
res1 = JSON.parse(JSON.stringify(result));
However, when I attempt to use the map function on this data, I encounter an issue:
res1.map() is not a function.
Why is this happening?
The map function should work on all array objects. It worked previously when I manually assigned the data to the variable like this res1=[{..}]
. But now it's not functioning properly.
When I console.log(result) I get the following output:
[ RowDataPacket {
device_id: '12335',
timestamp: 2018-05-14T08:31:23.000Z,
temperature: 21,
pressure: 31,
humidity: 20,
equipment_name: 'truck5' },
// more data rows displayed...
To resolve this, I used JSON.stringify(). Simply using JSON.parse() resulted in an error. Below is the MySQL query function I'm utilizing:
db.query(query,
function (err, result, fields) {
if (err) throw err;
var res1 =[];
res1=JSON.parse(JSON.stringify(result));
console.log(res1);
series:[
res1.map(function(s) {
// Meta data is necessary for tooltip plugin.
return {x: s.timestamp, y: s.temperature, meta: s.temperature + ' on ' + s.timestamp.toDateString()};
})
]
});
After checking the type of result or res, whether with or without JSON.parse(), I found that the type returned was object.