Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 由于未达成的承诺被拒绝而导致错误_Javascript_Node.js_Es6 Promise - Fatal编程技术网

Javascript 由于未达成的承诺被拒绝而导致错误

Javascript 由于未达成的承诺被拒绝而导致错误,javascript,node.js,es6-promise,Javascript,Node.js,Es6 Promise,我必须在一些调用的基础上在数据库中保存一些数据,所以我会使用承诺,但得到的错误是未处理的承诺拒绝,我不知道出了什么问题 我有一个控制器,首先我通过运行一个查询来检查一些东西,然后如果行存在,接下来我检查计数,如果计数为0,执行一个函数,其他函数要处理其他函数 我的控制器 exports.dispenceCashback = function(req, res) { const phoneNo = req.body.phoneNo const billAmt = req.body.

我必须在一些调用的基础上在数据库中保存一些数据,所以我会使用承诺,但得到的错误是
未处理的承诺拒绝
,我不知道出了什么问题

我有一个控制器,首先我通过运行一个查询来检查一些东西,然后如果行存在,接下来我检查计数,如果计数为0,执行一个函数,其他函数要处理其他函数

我的控制器

exports.dispenceCashback = function(req, res) {
    const phoneNo = req.body.phoneNo
    const billAmt = req.body.billAmt
    const cashBack = req.body.cashBack
    const billNo = req.body.billNo
    User.isValidConsumer(phoneNo)
        .then(([rows]) => {

        if (rows[0]) {   // if consumer valid / exist then go forward
            console.log(rows)
            let consumerName = rows[0].consumername

            User.checkRows(phoneNo)
                .then(([rows]) => {

                console.log(rows[0].count)
                if (rows[0].count == 0) {  // checking what is the count of row if 0 then executing this code
                    User.insertIntoDets(phoneNo, billAmt, cashBack, billNo, consumerName, retailerName, retailerId)
                    User.insertNew(phoneNo, billAmt, cashBack, consumerName)

                    } 
                    else {    //else do this functanility
                         User.insertIntoDets(phoneNo, billAmt, cashBack, billNo, consumerName, retailerName, retailerId)
                         User.update(phoneNo, billAmt, cashBack)
                    }
                })
                .catch(error => {
                    console.log(error)
                    return res.status(404).json({ error: 'Something went wrong' })
                })

            } 
            else {
                return res.status(404).json({ error: 'Ooooops!!!! invalid consumer' })
            }
        })
        .catch(err => console.log(err));
    }
关于上面的代码,这里是我运行查询的模型类

static isValidConsumer(phoneNo) {
    let sql = 'select *  from tpconsumer where consphoneno = ?'
    return db.execute(sql, [phoneNo]);
}

static checkRows(phoneNo) {
    let sql = 'select count(*) as count from cashbackdisp where consphoneno = ?'
    return db.execute(sql, [phoneNo]);
}

static insertIntoDets(phoneNo, billAmt, cashBack, billNo, consumerName, retailerName, retailerId) {

    console.log(retailerId, retailerName)
    let today = new Date();
    let dd = String(today.getDate()).padStart(2, '0');
    let mm = String(today.getMonth() + 1).padStart(2, '0');
    let yyyy = today.getFullYear();
    today = yyyy + '-' + mm + '-' + dd;

    let sql = 'INSERT INTO cashbackdispdets (consphoneno, consumername, businessid, businessname, billamt, cashbackamt, billno, billdate, billyear)VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?,?)'
    return db.execute(sql, [phoneNo, consumerName, retailerId, retailerName, billAmt, cashBack, billNo, today, yyyy, 'Y']);
}

static insertNew(phoneNo, billAmt, cashBack, consumerName) {
    let sql = 'INSERT INTO cashbackdisp (consphoneno, consumername, curyearpurchasedamt, curyearcashbackamt) VALUES (?, ?, ?, ?)'
    return db.execute(sql, [phoneNo, consumerName, billAmt, cashBack]);
}

static update(phoneNo, billAmt, cashBack) {
    let sql = 'UPDATE cashbackdisp SET  curyearpurchasedamt = curyearpurchasedamt+ ' + billAmt + ',curyearcashbackamt = curyearcashbackamt + ' + cashBack + ' WHERE consphoneno = ?'
    console.log("sql  :" + sql)
    return db.execute(sql, [phoneNo]);

}
我已经在我的控制器中注释了我正在做的事情,我得到的错误是:


列计数与值计数不匹配
。您在
插入债务
中有一个错误,此函数返回一个承诺,您没有该承诺的
。catch
,因此,您有一个
未处理的承诺拒绝
——所有这些都在错误消息中明确说明,无论您使用
User.insertIntoDets
User.insertNew
还是
User.update,如果失败,您都会捕获它们。除此之外,
insertIntoDets
您在其中使用了10个查询参数,并插入了9个字段。@JaromandaX您是说我必须捕获所有方法,如
User.insertIntoDets(phoneNo、billAmt、cashBack、billNo、consumerName、retailerName、retailerId)。然后(()=>{console.log(“saved”).catch(err=>{console.log(err)})
是的,但这不会修复代码中的错误-您仍然会收到错误