Reputation: 325
I'm developing a node.js app which connects into a MYSQL database, gets some info, converts it into JSON format and write it into the browser.
I'm using the mysql npm (https://www.npmjs.org/package/mysql).
connection.query("select tact, digits, mode from table1 where id = '"+id+"'", function(err, rows, fields){
if (err){
console.log(err);
throw err;
}
var objToJson = rows;
objToJson.response = response;
var finalresponse = JSON.stringify(objToJson);
});
And the final response is:
[{"tact":0,"digits":5,"mode":"on"}]
The point is that I only want to recieve something like (but it should be json parsed):
[{0,5,"on"}]
How could I do it? Is it possible?
Thanks guys.
Upvotes: 1
Views: 3676
Reputation:
According to W3Schools (https://www.w3schools.com/nodejs/nodejs_mysql_select.asp) the result can be queried like this:
console.log(result[2].address);
where address is a column in the dataset.
So since you have selected only 1 record:
let tact = result[0].tact;
let digits = result[0].digits;
let mode = result[0].mode
gives you the values you want as separate variable. or, you could put these into an array, and then work on.
Dermot
Upvotes: 0
Reputation: 341
To get each values into an array, iterate through it:
connection.query("select tact, digits, mode from table1 where id = '"+id+"'", function(err, rows, fields){
if (err){
console.log(err);
throw err;
}
var objToJson = rows;
var response = [];
for (var key in rows) {
response.push(rows[key]);
}
objToJson.response = response;
var finalresponse = JSON.stringify(objToJson);
});
Upvotes: 1