node js mysql accessing javascript object

node js mysql accessing javascript object

本文关键字:javascript object accessing mysql js node      更新时间:2023-09-26

我正在从 Node 查询数据库.JS 我正在尝试访问 CodeFile 中 FileContent 字段的内容,它返回一个 Javascript 对象: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);
});

控制台输出:[ RowDataPacket { FileContent: 'someContent' } ]

如果我尝试console.log(result.RowDataPacket);我会得到

控制台输出:undefined

如果我尝试console.log(result.RowDataPacket.FileContent);

整个节点崩溃

TypeError: Cannot read property 'FileContent' of undefined

我的最终目标是得到

控制台

输出:使用控制台.log(result.something.something)时someContent

我做错了什么?

看起来result是一个数组,尝试像这样访问它:

console.log(result[0].FileContent)