【问题标题】:Error when trying to get the value of a MAX query in node-mysql尝试在 node-mysql 中获取 MAX 查询的值时出错
【发布时间】: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


    【解决方案1】:

    已修复!我刚刚记得我过去遇到过类似的问题,我得到的答案在这种情况下也有效:How to access to the value when doing a COUNT() using node-mysql?

    还是很抱歉,谢谢!

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 2013-05-05
      • 1970-01-01
      • 2022-07-04
      相关资源
      最近更新 更多