2 回答
TA贡献1862条经验 获得超7个赞
建议的回调函数包含错误信息,如
// queries.js
const mysql = require('mysql');
const sql = require('./sql')
function GetAllChars(callback) {
sql.conn.query(`SELECT _Name FROM characters;`, function(error, results, fields) {
// change as
if(error) {
return callback(error)
}
callback(null,results); // returns RowDataPacket
})
}
exports.character = {
GetAllChars: GetAllChars,
}
在 index.js 上
...
function(req,res){
query.character.GetAllChars(function(err,data){
// check error info
if(err){
// console.debug(err)
return res.send(err.message)
}
res.json(data)
})
}
...
TA贡献1735条经验 获得超5个赞
将 CharList 放在 AllCharsToArray() 之外
// index.js
const sql = require('./sql')
const query = require('./queries');
query.character.GetAllChars(function(result) {
const CharList = [];
for (var i in result) {
CharList.push(result[i]._Name);
}
// launch the route after GetAllChars is done
expressApp.get('getCharList', (req, res) => {
res.json(CharList);
});
});
添加回答
举报