2
var usersRows = [];
connection.query('SELECT * from users', function(err, rows, fields) {
    if (!err) {
        rows.forEach(function(row) {
            usersRows.push(row);
        });
        console.log(usersRows);
    }
    else {
        console.log('Error while performing Query.' + err);
    }
});

It returned to me:

var usersRows = [ [ RowDataPacket { id: 1, name: 'sall  brwon', number: '+99999999\r\n' } ] ];

I need to parse this and remove rowdatapacket; I need result like this:

userRows = { id: 1, name: 'my name is', number: '+999999\r\n' };
Fayyaz Naqvi
  • 695
  • 7
  • 19
sall brown
  • 135
  • 2
  • 10

5 Answers5

4

If you need to get rid of RowDataPacket's array and save it in yours you can also use this:

usersRows = JSON.parse(JSON.stringify(results));

Vadim Shvetsov
  • 2,126
  • 2
  • 20
  • 28
2

Have you tried

userRows = RowDataPacket; 
Yokowasis
  • 363
  • 2
  • 11
0

You might want to try JSON.stringify(rows)

William Kheng
  • 671
  • 11
  • 15
  • That will return a string, when the OP is looking for the object. Plus, it's pretty costly to do this, compared to the accepted answer. – Michael Gaskill Jun 23 '16 at 01:45
0

Was unable to understand and implement the accepted the answer. Hence, tried the long way out according the results I was getting.

Am using "mysql": "2.13.0" and saw that the this library returned array of array out of which:

Index 0 had array of RowDataPackets 
Index 1 had other mysql related information.

Please find the code below:

var userDataList = [];
//Get only the rowdatapackets array which is at position 0
var usersRows = data[0];
//Loop around the data and parse as required
for (var i = 0; i < usersRows.length; i++) {
   var userData = {};
   //Parse the data if required else just
   userData.name = userRows.firstName + userRows.lastName;
   userDataList.push(userData);
}

If no parsing is required and you just want the data as is, you could use the solution like mentioned in link How do I loop through or enumerate a JavaScript object? Have not tried it but could be tweaked and worked out.

There should be simpler way to do it but as a novice I found the above way server the purpose. Do comment in case of better solution.

Community
  • 1
  • 1
Mrudang Vora
  • 255
  • 1
  • 5
  • 20
0

RowDataPacket is the class name of the object that contains the fields.

The console.log() result [ [ RowDataPacket { id: 1, name: 'sall brwon', number: '+99999999\r\n' } ] ] should be read as "an array of one item, containing and array of one item, containing an object of class RowDataPacket with fields id, name, and number"

Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80