代码停留在nodejs中的.push上,并且尝试以任何可能的方式将对象推入以便进一步使用的任何方式修改数组,因此不会执行任何进一步操作 这是一段代码
router.post('/addtocart', function (req, res, next) {
var cartadd = [];
addtocart = {
productId: req.body.productId,
product_name: req.body.pname,
product_price: req.body.price,
product_qty: req.body.adult,
vendor_id: req.body.vendor_id,
ticket_name: req.user.first_name,
ticket_email: req.user.email,
}
console.log(addtocart);
**cartadd.push(addtocart);**//stuck at this point
var addtocart = new Acart({
user: {
id: req.user._id,
first_name: req.user.first_name,
last_name: req.user.last_name,
email: req.user.email,
telephone: req.user.telephone
},
cart: cartadd,
paymentId: 1234, // Adding Dummmy payment id
checkin: req.body.arrival,
checkout: req.body.depart,
status: 'pending',
total: req.body.price
});
addtocart.save(function (err, orderdata) {
if (err) {
console.log("1196Error: " + err.message)
req.flash('error', 'Unable to save order... ' + err.message);
res.redirect('/');
}
req.flash('success', "Added to Cart!");
});
// return res.redirect('/');
});
答案 0 :(得分:0)
无法确定确切的错误,但是通过反复试验,此代码可以正常工作,但是,如果有人可以给出原因,那就没什么大不了了
router.post('/addtocart', function (req, res, next) {
var cartlist=[];
addtocart = {
productId: req.body.productId,
product_name: req.body.pname,
product_price: req.body.price,
product_qty: req.body.adult,
vendor_id: req.body.vendor_id,
ticket_name: req.user.first_name,
ticket_email: req.user.email,
}
console.log(addtocart);
cartlist.push(addtocart);
console.log("cartlist"+cartlist);
var addtocart = new Acart({
user: {
id: req.user._id,
first_name: req.user.first_name,
last_name: req.user.last_name,
email: req.user.email,
telephone: req.user.telephone
},
cart: cartlist,
checkin: req.body.arrival,
checkout: req.body.depart,
status: 'pending',
total: req.body.price
});
addtocart.save(function (err, orderdata) {
if (err) {
console.log("1196Error: " + err.message)
req.flash('error', 'Unable to Add to cart... ' + err.message);
res.redirect('/');
}
req.flash('success', "Added to Cart!");
console.log("success1193");
});
return res.redirect('/cart');
});
router.get('/cart', isLoggedIn, function (req, res, next) {
console.log(req.user._id);
qryFilter = { "user._id": req.user._id };
Acart.find(qryFilter, function (err, cart) {
console.log("cart1200"+cart);
res.render('shop/cart', {
layout: 'eshop/blank',
});
});
});