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' };
2
  • 2
    i cant find any solution... why so aggressive :( Commented Sep 27, 2015 at 9:25
  • for some reason, my comment has been erased... so the comment was: "and what have you tried?" Commented Sep 27, 2015 at 12:20

5 Answers 5

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));

Sign up to request clarification or add additional context in comments.

Comments

2

Have you tried

userRows = RowDataPacket; 

5 Comments

RowDataPacket is not defined
how is it not defined ? from what i see on your post its value is { id: 1, name: 'sall brwon', number: '+99999999\r\n'}
@Yokowasis: i need you, please help me.
@KumaresanPerumal why ? can't you just pm me ?
i got json from db with RowDataPacket. how to remove it?
0

You might want to try JSON.stringify(rows)

1 Comment

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.
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.

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.