Nodejs + MySQL错误:握手不活动超时

时间:2018-07-10 18:41:49

标签: mysql node.js express session express-session

我正在开发一个书店应用程序,我正在尝试将书籍添加到购物车,但出现此错误消息“错误:握手不活动超时”

当我通过控制台记录错误时,价格和总价未得到评估

这是我的cart.js模型

module.exports = function Cart(oldCart) {
this.items = oldCart.items || {};
this.totalQty = oldCart.totalQty || 0;
this.totalPrice = oldCart.totalPrice || 0;

//adding an item to cart
this.add = function (item, id) {
    var storedItem = this.items[id];

    if (!storedItem) {
        storedItem = this.items[id] = {item: item, qty: 0, price: 0};
    }

    storedItem.qty++;
    storedItem.price = storedItem.item.price * storedItem.qty;

    this.totalQty++;
    this.totalPrice += storedItem.item.price;
}

//Make order array based on item id
this.generateArray = function(){
    var arr = [];

    for(var id in this.items){
        arr.push(this.items[id]);
    }

    return arr;
}
}

这是获取请求

/*GET add-to-cart page*/
router.get('/add-to-cart/:id', function(req, res, next){
var productID = req.params.id;
var cart = new Cart(req.session.cart ? req.session.cart : {});

var sql = `SELECT * FROM books WHERE BookID = ${productID}`;
db.queryRow(sql, function (err, book) {
/*if (err) { 
    res.redirect('/'); 
}*/
    console.log(book);
    cart.add(book, book.BookID);
    console.log(book.BookID);
    req.session.cart = cart;
    console.log(req.session.cart);
    res.redirect('/');
});
});

我该如何修理我的购物车以计算价格和总价并解决此错误?

0 个答案:

没有答案