I am trying to insert the request.body I receive which is struct the following into database
request.body = [{
"createdAt": "2019-03-11T08:03:11.438",
"deviceType": "type1",
"deviceSerial": "123",
"metricName": "metric1",
"metricValue": "29"
},
{
"createdAt": "2019-03-11T08:03:11.442",
"deviceType": "type2",
"deviceSerial": "234",
"metricName": "metric2",
"metricValue": "29"
},
{
"createdAt": "2019-03-11T08:03:11.442",
"deviceType": "type3",
"deviceSerial": "345",
"metricName": "metric3",
"metricValue": "165"
}
]
Trying to set the request.body into database:
var mysql = require('node-mysql');
var conn = mysql.createConnection({
...
});
var sql = "INSERT INTO Test (created_at, device_type, devices_serial, metric_name, metric_value) VALUES ?";
var values = request.body //
conn.query(sql, [values], function(err) {
if (err) throw err;
conn.end();
});
I am trying to understand how to construct the var values so it will fit to the following format:
var values = [
['2019-03-11T08:03:11.438', 'type1', '123', 'metric1', '29'],
['2019-03-11T08:03:11.438', 'type2', '234', 'metric2', '29'],
['2019-03-11T08:03:11.438', 'type3', '345', 'metric3', '165'],
];