Javascript 当我的购物车只包含一个项目时,Ajax没有响应
在我的购物车页面中,当用户选择的产品数量小于1时,我试图删除一个产品。这种变化也应反映在产品总量上。我使用ajax就是为了这个目的,它只在购物车包含多个项目时删除产品,而不是在单个项目的情况下删除产品 1.路线 2.changeProductQuantity()函数 3.Ajax 4.输出屏幕(成功案例和失败案例) 在这张图中,您可以看到用户的购物车中有多个产品。当用户更改第二个产品数量时,“qtyChange”对象变为真,而当数量达到零时,他/她单击(-)按钮“removeProduct”变为真。我已将这些更改标记在此图像的右侧 在这张图片中,你们可以看到我试图将数量从1减少到零。在我前面提到的这种情况下,应该删除它。但它并没有删除。它不会像您在前一幅图中看到的那样显示removeProduct对象。当它发生时,我在控制台中得到以下错误Javascript 当我的购物车只包含一个项目时,Ajax没有响应,javascript,ajax,mongodb,handlebars.js,Javascript,Ajax,Mongodb,Handlebars.js,在我的购物车页面中,当用户选择的产品数量小于1时,我试图删除一个产品。这种变化也应反映在产品总量上。我使用ajax就是为了这个目的,它只在购物车包含多个项目时删除产品,而不是在单个项目的情况下删除产品 1.路线 2.changeProductQuantity()函数 3.Ajax 4.输出屏幕(成功案例和失败案例) 在这张图中,您可以看到用户的购物车中有多个产品。当用户更改第二个产品数量时,“qtyChange”对象变为真,而当数量达到零时,他/她单击(-)按钮“removeProduct”变
它说总数是未定义的。为什么会这样?你在哪里犯的错误?请帮助..使用try-catch,如果响应不是从promise返回的,则创建自己的响应并返回数据
router.post('/change-product-quantity', async (req, res, next) => {
try {
let response = await userHelpers.changeProductQuantity(req.body)
if(response) {
response.total = commaNumber(
await userHelpers.getTotalAmount(req.body.user)
)
} else {
response = {}
response.total = commaNumber(
await userHelpers.getTotalAmount(req.body.user)
)
}
res.json(response)
} catch(error) {
res.json({ total: 0 })
}
})
使用try-catch,如果响应不是从promise返回,则创建自己的响应并返回数据
router.post('/change-product-quantity', async (req, res, next) => {
try {
let response = await userHelpers.changeProductQuantity(req.body)
if(response) {
response.total = commaNumber(
await userHelpers.getTotalAmount(req.body.user)
)
} else {
response = {}
response.total = commaNumber(
await userHelpers.getTotalAmount(req.body.user)
)
}
res.json(response)
} catch(error) {
res.json({ total: 0 })
}
})
您的代码在这里出错
{$pull:{products:{item:objectId(details.product)}}
。其中response未定义。response.total给出未定义的总数。您需要在所有情况下使用try-catch或reject/resolve-from-promise。您能分享一下如何做到这一点吗?我是nodejs的新手,所以不知道如何以正确的方式编写这些内容。如果您能提供一个模型,我可以自己编写。您做得很好。code write是随着实践和时间而来的艺术。即使我需要如此多的改进。我是一个糟糕的老师。当产品的数量等于1时,我无法删除产品的原因是因为变量total未定义。使用catch块处理此错误将解决此问题。但我不知道如何处理此问题。a你的代码在这里出现了错误{$pull:{products:{item:objectId(details.product)}
。其中response未定义。response.total给出未定义的总数。您需要在所有情况下使用try-catch或reject/resolve-from-promise。您能分享一下如何做到这一点吗?我是nodejs的新手,所以不知道如何以正确的方式编写这些内容。如果您能提供一个模型,我可以自己编写。您做得很好。code write是随着实践和时间而来的艺术。即使我需要如此多的改进。我是一个糟糕的老师。当产品的数量等于1时,我无法删除产品的原因是因为变量total未定义。使用catch块处理此错误将解决此问题。但我不知道如何处理此问题。a有人帮我吗?我需要在承诺中包含拒绝吗?没有必要。但这是良好的行为准则和良好实践。我需要在承诺中包含拒绝吗?没有必要。但这是良好的行为准则和良好实践。
function changeQuantity(cartId, proId, userId, count) {
count = parseInt(count)
let quantity = parseInt(document.getElementById(proId).innerHTML)
let qty = quantity + count
console.log("Qty:" + qty)
console.log("Count" + count)
$.ajax({
url: '/change-product-quantity',
data: {
cart: cartId,
product: proId,
user: userId,
count: count,
quantity: qty
},
method: 'post',
success: (response) => {
console.log(response)
if (response.removeProduct) {
setTimeout(function() { // wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 5000);
} else {
document.getElementById(proId).innerHTML = quantity + count
document.getElementById('total').innerHTML = response.total
}
}
})
}
router.post('/change-product-quantity', async (req, res, next) => {
try {
let response = await userHelpers.changeProductQuantity(req.body)
if(response) {
response.total = commaNumber(
await userHelpers.getTotalAmount(req.body.user)
)
} else {
response = {}
response.total = commaNumber(
await userHelpers.getTotalAmount(req.body.user)
)
}
res.json(response)
} catch(error) {
res.json({ total: 0 })
}
})