TypeError: Cannot read property on my nodejs API

January 28, 2021, at 12:50 PM

Here is my code

async totalSum(req, res) {
    let userId = req.params.userId;
    let user = await User.findOne({userId:userId});
    if (user == null) {
      res.status(400).json({message:"User not found"});
    let cartItem = await CartItem.find({
      userId: userId
    var totalSum = 0;
    var x = 0;
    while (cartItem[x].productId) {
      let productId_x = cartItem[x].productId;
      console.log(typeof productId_x);
      let productId_x_quantity = cartItem[x].quantity;
      let product = await Product.findOne({
        productId: productId_x
      let productId_x_price = product.price;
      let mul_product = math.evaluate( productId_x_price * productId_x_quantity );
      console.log(typeof mul_product);
      totalSum += mul_product;
    return res.status(200).json({Total: totalSum});

I'm developing a eCommerce site, here the API is to calculate the total sum in cart items. First through params it will get the userId and finds the account in mongodb and then it finds the cart items from another collection, since it would be more item, I'm taking the values as a array and passing in a while loop. it will use the productId to find the price and calculate the total. I'm able to calculate the total and check it via logging but when api is testing using POSTMAN I'm getting a error

TypeError: Cannot read property 'productId' of undefined
 at totalSum (/home/abhi/getmayon/ecom/blockchainecommerce/loyalty-app-2/controllers/users.js:269:24)
 at processTicksAndRejections (internal/process/task_queues.js:93:5)
Answer 1

Your While loop is running infinitive time as though x counter is just increasing there is no condition for stoping. I will suggest you to use map. as though you have got CartItem so value are present here . just map through the CartItem.

  let productId_x = item.productId;
  let productId_x_quantity = item.quantity;
  let product = await product.findOne({
    productId : productId_x
 ...rest of code 
Python 2.7 fnmatch NOT editing text

Python 2.7 fnmatch NOT editing text

I have a file with 600k+ records of string labels I am trying to edit with an update cursor using both the string modules and fnmatch to find patterns to editThe section using fnmatch is successfully printing the matched records but not changing / parsing...

How to build a known apk for play store in flutter?

How to build a known apk for play store in flutter?

i made app and build it as an apk file in flutter and i tried to install it in my android phone but while installing it, play store said: the app is unkhown do you wanna install, and then i installed the app and app was working well, the question is how i can solve it, Do i have to buy a play store account?...

Unable to start Hive metastore related to java exception

Unable to start Hive metastore related to java exception

I installed Apache hive 3, Apache Hadoop 3, Trino and Mysql (mysql Ver 1414 Distrib 5

Select content from “div class” which contains certain words

Select content from “div class” which contains certain words

I have the following ajax-response which is generated by a server sided script: