Database query
function query(sql) {
return new promise((resolve, reject) => {
pool.getConnection(function (err, conn) {
if (err) {
reject(err);
} else {
conn.query(sql, function (err, rows, cols) {
if (err) {
conn.release();
reject(err)
} else {
//
conn.release();
//
resolve({ rows: rows, cols: cols });
}
});
}
});
});
};
exports.myTickets = (userId, cb) => {
let sql = util.format("select ticketId,number from ticket_Numbers where userId="%s"", userId);
db.query(sql)
.then((data) => {
let tickets = data.rows;
tickets.map(ticket => {
db.query(util.format("select userId,number from ticket_numbers where ticketId="%s"", ticket.ticketId))
.then((players) => {
ticket.players = players.rows;
});
})
console.log(tickets); //logplayers
console.log(JSON.stringify(tickets)); //stringify players
cb({ success: true, tickets: tickets });
})
.catch(err => {
console.log(err.stack);
cb({ success: false, message: err.message });
});
}
result:
novice to node, could you tell me why this happened and what went wrong?