最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access RowDataPacket mysql-node.js - Stack Overflow

programmeradmin4浏览0评论
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?

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?

Share Improve this question edited Jun 30, 2016 at 21:02 Arihant asked Jun 30, 2016 at 20:54 ArihantArihant 4,04718 gold badges58 silver badges91 bronze badges 1
  • Possible duplicate of How to access a RowDataPacket object – Damjan Pavlica Commented Aug 1, 2018 at 17:20
Add a comment  | 

3 Answers 3

Reset to default 18

Try this:

results[0].@update_id

use console.log(JSON.stringify(result));

then if you want to get access data on variable

var data = JSON.stringify(result[0].@update_id)
console.log(data);

you can't use index on var E.G

console.log(data[0].@update_id) it is wrong

just conver the result to array, then you can easily access the data, i get solved by doing this

let res = JSON.parse(JSON.stringify(result));
发布评论

评论列表(0)

  1. 暂无评论