当我尝试从MySQL更新数据库时,为什么会显示此错误?

时间:2019-11-15 23:46:09

标签: javascript mysql node.js

我收到此错误,我无法弄清楚。

      throw err; // Rethrow non-MySQL errors
      ^
TypeError: this._callback.apply is not a function

我正在将此命令行应用程序用于我的编码训练营的家庭作业。直到最后一条else语句,大部分代码都在工作。那就是抛出错误的地方。我不是很了解错误。我必须这样做,以便在MySQL数据库中更新数量。

    inquirer
        .prompt([
            {
                name: "choice",
                type: "input",
                message: "What is the ID# of the product you would like to purchace?",
            }, {
                name: "qtySelection",
                message: "How many units of would you like?",
                type: "input"
            }
        ]).then(function (result) {

            var query = "SELECT * FROM products WHERE item_id = ?";
            connection.query(query, result.choice, function (err, res) {
                if (err) throw err;
                if (!res.length) {
                    console.log("\r\n");
                    console.log("Sorry item not in inventory, please try again.");
                    setTimeout(userPurchase, 1000);
                } else {
                    if (result.qtySelection > res[0].stock_quantity) {
                        console.log("\r\n");
                        console.log("Sorry we do not enough have enough in stock! PLeas try again!");
                        setTimeout(userPurchase, 1000);
                    } else {

                        var newQty = parseInt(res[0].stock_quantity) - parseInt(result.qtySelection)
                        connection.query("UPDATE products SET stock_quantity = ? WHERE item_id = ?", newQty, result.choice, function (err, result) {
                            // if (err) throw err;
                            console.log("Thank you for purchasing " + result.qtySelection + "of " + result.choice + ". Your total is $" + parseInt(result.qtySelection) * parseInt(res[0].price))

                        })
                    }
                    connection.end();
                }
            })
        })
}

0 个答案:

没有答案
相关问题