1

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.

2 Answers 2

1

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);
});
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.