【发布时间】:2020-03-11 11:30:33
【问题描述】:
我需要从我的数据库表中获取最后一个id,所以我必须使用MAX才能实现它,所以这是我正在使用的sql查询:SELECT MAX(id) FROM payments
当我尝试从对象中获取值时出现问题,因为它抛出了这个错误:id is not defined
server.js:
app.post('/sendMail', function(req, res) {
getLastPaymentId().then((data) => {
lastPaymentId = data.lastPaymentId;
console.log(lastPaymentId.MAX(id)); //here I want to get the value
});
res.redirect('/');
});
function getLastPaymentId() {
const idPayment = new Promise((resolve, reject) => {
dbConnection.getLastPaymentId().then(data => {
resolve(data)
})
});
return Promise.all([idPayment]).then(data => {
return {
lastPaymentId: data[0]
}
})
}
dbConnection.js:
function getLastPaymentId() {
return new Promise(function(resolve, reject) {
const sql = "SELECT MAX(id) FROM payments";
con.query(sql, function(err, result) {
if(err) throw err;
resolve(result);
});
});
}
module.exports.getLastPaymentId = getLastPaymentId;
仅打印对象时的结果 (console.log(lastPaymentId)):
它是null,因为我的数据库中还没有行。
打印值时的结果(console.log(lastPaymentId.MAX(id))):
它应该打印null。
我该如何解决这个问题?
【问题讨论】:
标签: node.js node-mysql