Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access RowDataPacket mysql-node.js

connection.query('
SET @update_id := 0; 
UPDATE relations set x1 = ?, y1 = ?, id = (SELECT @update_id := id) WHERE element_to =?;
SELECT @update_id;',[data.x2,data.y2,id],function(err,result){
if(err){console.error(err);}
else{console.log(result);console.log(result.@update_id);}
});

I am getting the following result from a query execution:

[ RowDataPacket {
  '@update_id': 'WAbWA1WA5WA2WA8WAdWA4WA9' } ] ]

How do I access the @update_id field to store the value in a variable. I know these are objects and I tried the following to access them such as follows:

results.@update_id; 

But I get undefined when I try to log the value. How to get the value?

like image 953
Arihant Avatar asked Jun 30 '16 20:06

Arihant


People also ask

How do I connect to MySQL node Express?

const connection = mysql. createConnection({ host: 'localhost', // host for connection port: 3306, // default port for mysql is 3306 database: 'test', // database from which we want to connect out node application user: 'root', // username of the mysql connection password: 'root' // password of the mysql connection });

What is RowDataPacket?

RowDataPacket is actually the name of the constructor function that creates an object, it would look like this new RowDataPacket(user_id, ...) . You can check by accessing its name [0].constructor.name. If the result is an array, you would have to use [0].

Is Node JS good with MySQL?

Generally, Node. js is coupled with MongoDB and other NoSQL databases, but Node. js performs well with relational databases like MySQL, too. If you want to write a new microservice with Node.


1 Answers

Try this:

results[0].@update_id
like image 161
Lalit Goswami Avatar answered Oct 31 '22 22:10

Lalit Goswami