大家好,我知道我的問題很容易購買我是新的使用json。節點json請求堆棧
var mysql = require('mysql');
function myCon(sql){
var con = mysql.createConnection({
host: "127.0.0.1",
port: "3306",
user: "root",
password: "xxxxxx",
database: "myDataBase",
});
// here i'm passing sql query as var sql
con.query(sql, function(err, rows) {
if (err) { throw err }
else {
console.log(rows);
// here I want my response to be json response eg { key:value}
}
})
con.end();
}
module.exports.con = myCon;
結果是
[ RowDataPacket { countryId: 1, countryName: 'N/A' },
RowDataPacket { countryId: 2, countryName: 'UK' },
RowDataPacket { countryId: 3, countryName: 'USA' },
RowDataPacket { countryId: 4, countryName: 'UAE' } ]
我想要的響應是作爲
{ countryId: 1, countryName: 'N/A' },
{ countryId: 2, countryName: 'UK' },
{ countryId: 3, countryName: 'USA' },
{ countryId: 4, countryName: 'UAE' }
與出RowDataPacket 並將其重新發送到EJS視圖
你的結果看起來像無效的JavaScript,是你的日誌 – adeneo
中得到真正地是什麼我傳遞從SQL其他js文件var country = mysql.con('select countryId,countryName from country'); –
您正在使用哪個MySQL模塊? – Paul