How to parse the data from "rows" object in node.js,express.js,mysql2
Try this (this is really basic):
connection.query('SELECT * from pass', function(err, rows) {
res.json(rows);
var user = rows[0].userid;
var password= rows[0].password;
});
connection.query('SELECT * from pass', function(err, rows) {
data = rows[0];
let user = data.userid;
let password= data.password;
res.json(rows);
});
[{"userid": "test","password": "test"}]
This is an Array
of Object
s. So: First loop over the array to get a single object and then extract its properties:
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
console.log(row.userid);
}