Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node js mysql accessing javascript object

I am querying a db from Node.JS I am trying to get access to the content of the FileContent field in CodeFile, it returns a Javascript object: result.

jsUpdateCon.query('SELECT FileContent FROM codeFile WHERE ID = ?',[msg[1]], function(err, result){
if (err) throw err;
console.log('Data received from DB:\n');
    console.log(result);
});

console output: [ RowDataPacket { FileContent: 'someContent' } ]

if I try console.log(result.RowDataPacket); I get

console output: undefined

if I try console.log(result.RowDataPacket.FileContent);

The entire node crashes with

TypeError: Cannot read property 'FileContent' of undefined

my ultimate goal would be to get

console output: someContent when using console.log(result.something.something)

What am I doing wrong?

like image 250
shaun Avatar asked Mar 01 '26 19:03

shaun


1 Answers

It looks like the result is an array, try to access it like this:

console.log(result[0].FileContent)
like image 145
madox2 Avatar answered Mar 03 '26 08:03

madox2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!