Node.js… How to delete ambiguous cart items

I use MongoDB+Mongoose with Node.js. The question I’m facing is… If the person who set some kind of product into the cart, deleted the project for some reason, and the buyer who applied this item to the cart before the item was deleted comes to the cart, the program fails because the cart array is corrupted since there is no such product anymore…

My idea and it might be a bad idea, but nevertheless, was when the buyer click to the link to the cart page, node.js in the backend checks all products, matches it with the products in the cart, and each time it finds match pushes this match into array and then this array we match with the request. It might be a horrible idea, because the time of the request so far was quite long, and besides I cannot finish my asynchronous method… So far I done this…:

mongo db object structure for product:

{"_id":{"$oid":"61cc5e20ace8b042b739e8e3"},
"title":"fhgfjhgfjhj",
"price":12,"description":"hgfjhfjhfjhf",
"imageUrl":"images\20211229T130952.jpg",
"userId":{"$oid":"61cc5df4ace8b042b739e8d9"},"__v":0}

mongodb structure for the user which includes the cart:

{"_id":{"$oid":"61cc5df4ace8b042b739e8d9"},
"email":"[email protected]",
"password":"$2a$12$FUrAK/E8AtSvadX5m45BNu086/5MVasOAsdvwjdwv6KClpxgHJHh.",
"cart":{"items":[{"productId":{"$oid":"61cc5e20ace8b042b739e8e3"},"quantity":3,"_id":{"$oid":"61cc62c9ce1642aea120dacd"}},{"productId":{"$oid":"61cc642af0886f1e3086ff3e"},"quantity":1,"_id":{"$oid":"61cc88b19101f6fbde25c1e7"}}]},"__v":5}

exports.getCart = (req, res, next) => {
  const unchecked_products = req.user.cart.items;
  const checked_cart = [];
  Product.find()
    .then((products) => {
      products.forEach((product) => {
        unchecked_products.forEach((unchecked_product) => {
          if (
            unchecked_product.productId.toString() === product._id.toString()
          ) {
            checked_cart.push(unchecked_products);
          }
        });
      });
      return (req.user.cart.items = checked_cart);
    })
    .then((result) => {
      req.user
        .populate("cart.items.productId")
        .then((user) => {
          const products = user.cart.items;
          res.render("shop/cart", {
            pageTitle: "Your Cart",
            path: "/cart",
            products: products, //cartProducts
          });
        })
        .catch((err) => {
          console.error(err);
          const error = new Error(err);
          error.httpStatusCode = 500;
          return next(error);
        });
    });

I make raw mistake somehow. I tried to match and form new array for the cart… req.user.cart and checked_cart after I pushed there objects, they are one to one match structurally to each other, so I think and it suppose to work like this I will just equal them so I renew req.user.cart to the new data which is not corrupted. (All object which are deleted from products are ignored and not pushed to the new array of objects)…

And then I would do: req.user.populate(‘cart.items.productId’)… But something happens at this point and I lose my array and data becomes empty…

I use async with then blocks… And I tried to set req.user… inside and outside then block, but nothing good happens… I’m studying node.js and obviously I don’t see some mistake I made…