我创建了一个函数来返回数据库列值。该函数返回值很好。但这不是我想要的格式。我已经通过代码在下面解释了我的问题。
功能:
async function getHtmlNoteContent(quote_id){
try{
const connection = await mysql.createConnection(config.mysql.credentials);
const [note] = await connection.query(`select notes_html from table where id = ${quote_id}`);
connection.end();
console.log('ppppp -->',JSON.stringify(note));
return JSON.stringify(note);
}catch (e) {
utils.error500(req, res, e.message);
}
}
上面的函数返回值是这样的-->
ppppp --> [{"notes_html":"<p>column value</p>"}]
但是我想要-->
ppppp --> <p>column value</p>
有人可以告诉我该怎么做吗?谢谢
最佳答案
您可以清楚地看到您的 note
变量当前包含一个包含 1 个对象的数组,并且该对象具有您要返回的 notes_html
属性。
首先,您需要通过以下方式访问您执行的对象:note[0]
。其次,您只想获取该对象的 notes_html
属性,可以通过以下方式实现:note[0]['notes_html']
或 note[0] .notes_html
.
所以,而不是:
return JSON.stringify(note);
这样做:
return note[0].notes_html; // or note[0]['notes_html']
关于javascript - 如何仅从 Nodejs 中的数据库列值返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62818373/