ani-atl
ani-atl

Reputation: 23

Json object access in Node server

I am new to Node server. In my program I fetched some data from data base as

connection.query("SELECT * from login where username='" + uname + "' and password='" + pwd + "' ", [uname, pwd], function (err, rows)
    {
        var employees = (JSON.stringify(rows));
        console.log("Inside server "+employees  )
    });  

Now in console i got some thing like

[{"user_id":7,"username":"vb","password":"vbv"}]

Now my Question is how i could get the value username from employees. I have tried few things like employees[0]["username"] OR employees.username etc..

Upvotes: 0

Views: 32

Answers (2)

Cyrbil
Cyrbil

Reputation: 6478

JSON.stringify(rows) only allow you to pretty print the data.

To access it you were right with employees[0]["username"]

connection.query("SELECT * from login where username='" + uname + "' and password='" + pwd + "' ", [uname, pwd], function (err, employees)
    {
        console.log("Inside server " + JSON.stringify(employees));
        console.log('Employee 1: ' + employees[0]["username"]);
    });  

Upvotes: 0

Tuan Anh Tran
Tuan Anh Tran

Reputation: 7237

Because you stringify it, it becomes string so you can't access like that. Just use it as is.

Upvotes: 1

Related Questions