Thursday 8 June 2017

How to iterate though specific attributes of RowDataPacket using node.js

I asked the question before here and before I could explain it properly, it was marked as a duplicate due to my lack of understanding the situation well that time.

My attempt using stringify:

connection.query("select * from DonateTo where UserId ="+ req.user.UserId ,function(err,rows){
                console.log('rows[0]:');
                console.log(console.log(JSON.stringify(rows[0])));
                var row = JSON.stringify(rows[0]);

                console.log(rows);

                for (var name in row) {  
                    console
                    console.log(row[name]);
                    if(row[name]==null)
                    {
                        console.log("update DonateTo set CauseId"+i+" = "+id);
                        connection.query("update DonateTo set CauseId"+i+" = "+id);
                    }

                }
            });

My attempt using keys:

var keys = Object.keys(rows);
            for (var i = 1; i < 6; i++)
            {   
                console.log(keys[i],rows[keys[i]]);
                if(keys[i],rows[keys[i]]==null)
                {
                    console.log("update DonateTo set CauseId"+i+" = "+id);
                    connection.query("update DonateTo set CauseId"+i+" = "+id);
                }    
            }

I clearly don't understand how RowPacketData works.
Also, I only want to iterate through the 2nd to 5th objects.



via Ishaan Shakunt

No comments:

Post a Comment