Nirodha Wickramarathna
Nirodha Wickramarathna

Reputation: 291

How to get data from JSON Nodejs?

Code:

var email = req.body.email
var sql = 'Select password FROM user where email = ' + mysql.escape(email)
con.query(sql, (err, rows, fields) => {
  if (!err) {
    res.send(rows)
    var result = Object.values(JSON.parse(JSON.stringify(rows)))

    console.log(result)
  } else console.log(err)
})

Here, When I print the 'result' variable I can get output like below

[{ password: '123' }]

How I get password value to another variable (Example: x=123)

Upvotes: 0

Views: 122

Answers (1)

Mattia Paletti
Mattia Paletti

Reputation: 51

Your password is stored as an object in the first cell of an array, therefore:

var result = [{ password: '123' }];
var x = result[0].password; //Get the password value of the object in the first cell of the array

console.log('Result = ' + JSON.stringify(result));
console.log('x = ' + x);

Upvotes: 1

Related Questions