How to parse the data from “rows” object in node.js,express.js,mysql2

前端 未结 3 1388
我寻月下人不归
我寻月下人不归 2021-02-07 10:59

I m using the node,express,mysql2 packages .When i m using console.log(rows) ,it is giving me following output:

[{\"userid\": \"test\",\"password\": \"test\"}]
<         


        
相关标签:
3条回答
  • 2021-02-07 11:14
    [{"userid": "test","password": "test"}]
    

    This is an Array of Objects. 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);
    }
    
    0 讨论(0)
  • 2021-02-07 11:24

     connection.query('SELECT * from pass', function(err, rows) {
          data = rows[0];
         let user = data.userid;
         let password= data.password;
         res.json(rows);
    
       });

    0 讨论(0)
  • 2021-02-07 11:26

    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;
    
       });
    
    0 讨论(0)
提交回复
热议问题