Javascript 将元素从mongodb推送到数组时出错
使用for循环将元素推送到数组时发生Geting错误 我的代码Javascript 将元素从mongodb推送到数组时出错,javascript,node.js,arrays,for-loop,object,Javascript,Node.js,Arrays,For Loop,Object,使用for循环将元素推送到数组时发生Geting错误 我的代码 for(numberOfProducts=0;numberOfProducts) router.get('/cart', verifyLogin, async (req, res) => { var products = await userHelpers.getCartProducts(req.session.user._id) console.log("quantity of 1 pro
for(numberOfProducts=0;numberOfProducts)
router.get('/cart', verifyLogin, async (req, res) => {
var products = await userHelpers.getCartProducts(req.session.user._id)
console.log("quantity of 1 product:"+products[0].quantity);
console.log("price of 1 product"+products[0].product.Price);
console.log("length of cart:"+products.length);
var numberOfProducts
var proNum=[]
var productsList=0
for(numberOfProducts=0 ; numberOfProducts<=products.length; numberOfProducts++)
{
console.log("price of product ****"+ products[numberOfProducts].product.Price);
proNum[productsList]=products[numberOfProducts].product.Price
productsList++
}
let totalValue=proNum.reduce((accumulator,currentValue)=>{
return accumulator+currentValue
},0)
/////////
res.render('user/cart', { products, user: req.session.user, totalValue })
}
quantity of 1 product:3
price of 1 product100000
length of cart:3
price of product ****100000
price of product ****65000
price of product ****6509
(node:6604) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'product' of undefined
at C:\Users\Bimal Boby\Desktop\E-commerce-Website\routes\user.js:107:71
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:6604) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6604) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
for(numberOfProducts=0 ; numberOfProducts<=products.length; numberOfProducts++)
for(numberOfProducts=0 ; numberOfProducts < products.length; numberOfProducts++)