2

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 // <- Here it the problem, I guess

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'],
];
2
  • What's the problem? Do you get any error? Is request defined at this point? Commented Mar 11, 2019 at 8:22
  • This solution works but probably not the best. generate sql like this 'Insert into...... values (?,?,?...?), (?,?,?...?)...' . Each () contains a record in database. And then spread params sequently. Commented Mar 11, 2019 at 8:38

3 Answers 3

1

You can get the object values as Array

var values = request.body.map(r=>  Object.values(r));
Sign up to request clarification or add additional context in comments.

Comments

1

EDIT : Try this

    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.map(Object.values);

    conn.query(sql, [values], function (err) {
        if (err) throw err;
        conn.end();
    });

Comments

0

Something like this should work:

var conn = mysql.createConnection({
    ..
});

let request = {};
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"
  }
];

// Convert to array of arrays. 
const values = request.body.map((obj) => Object.values(obj));

const sql = "INSERT INTO Test (created_at, device_type, devices_serial, metric_name, metric_value) VALUES ?";

conn.query(sql, [values], function(err) {
    if (err) throw err;
    conn.end();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.