title: Trying to add a product to list in NodeJS ("TypeError undefined")
body: <p>I have just started NodeJS, so I'm on a beginner level. I am trying to build a shopping list app with MongoDB, in which user logs in, creates a list and then adds items to that list. I can register &amp; log in and create a list, but when I try to add items to it, that's when I run into this:
  "TypeError: Cannot read property 'products' of undefined".
  Code for it is:</p>
  
  <pre><code>    const user = req.user;
      const shoppinglist_id = req.body.shoppinglist_id;
      const name = req.body.name;
  
      let new_product = product_model({
          title: req.body.title,
          imagePath: req.body.imagePath,
          quantity: req.body.quantity
      });
      new_product.save().then(() =&gt; {
          shoppinglist_id.products.push(new_product);
          console.log('product saved');
          shoppinglist_id.save().then(() =&gt; {
              return res.redirect('/');
          });
      }); ```
  
  User model:
  const user_schema = new Schema({
      name: {
          type: String,
          required: true
      },
      shoppinglists: [{
          type: mongoose.Schema.Types.ObjectId,
          ref: 'shoppinglist',
          req: true
      }]
  });
  
  Shoppinglist model:
  const shopping_list_schema = new Schema({
      name: {
          type: String,
          required: true
      },
      products: [{
          type: mongoose.Schema.Types.ObjectId,
          ref: 'product',
          req: true
      }]
  });
  
  Product model:
  var schema = new Schema({
     imagePath: {type: String, required: true}, 
     title: {type: String, required: true},
     quantity: {type: Number, required: true}
  });
  </code></pre>
  
  <p>Any idea what I'm doing wrong? I know the problem is with this line:
  "shoppinglist_id.products.push(new_product);", I have tried everything on it, "user.shoppinglists.products" etc. Nothing works. Any help?</p>
  
  <p>EDIT:
  I'll also post as a reference my add shoppinglist, which is working fine.</p>
  
  <pre><code>    const user = req.user;
      let new_shoppinglist = shopping_list_model({
          name: req.body.name,
          products: req.body.products
      });
      new_shoppinglist.save().then(() =&gt; {
          console.log('shoppinglist saved');
          user.shoppinglists.push(new_shoppinglist);
          user.save().then(() =&gt; {
              return res.redirect('/');
          });
      });
  }```
  
  
  EDIT #2:
  I am adding how I am getting to shoppinglist-view:
  ´´´
  const get_shoppinglist = (req, res, next) =&gt; {
      const shoppinglist_id = req.params.id;
      shopping_list_model.findOne({
          _id: shoppinglist_id
      }).then((shoppinglist) =&gt; {
          shoppinglist.populate('products')
          .execPopulate()
          .then((shoppinglist) =&gt; {
              let data = {
                  shoppinglist: shoppinglist
  
              };
              console.log(data);
              let html = shoppinglist_views.shoppinglist_view(data);
              res.send(html);
          });
      });
  };´´´
  
  And shoppinglist-view:
  ´´´
  const shoppinglist_view = ((data) =&gt; {
      let html = `
      &lt;html&gt;
      &lt;body&gt;
      &lt;h1&gt;&lt;/h1&gt;
      &lt;h2&gt;${data.shoppinglist.name}&lt;/h2&gt;
      &lt;h4&gt;&lt;a href="/"&gt;Go back&lt;/a&gt;&lt;/h4&gt;
  
  
      &lt;br&gt;
      `
       data.shoppinglist.products.forEach((product) =&gt; {
          html += products
           html += `
           &lt;div&gt;
           &lt;p&gt;
           &lt;h2&gt;Name of the shopping list: ${shoppinglists.shoppinglist.name}&lt;/h2&gt;
           &lt;h3&gt; Name: ${product.title}&lt;br&gt;&lt;/h3&gt;
           &lt;img src="${product.imagePath}" width="50px" height="50px" /&gt;
           quantity: ${product.quantity} &lt;/p&gt;
           &lt;/div&gt;
           &lt;/body&gt;
           &lt;/html&gt;
           `;
          });
          html += `
          &lt;form action="/add-product" method="POST"&gt;
          &lt;p&gt;Add products&lt;/p&gt;&lt;br&gt;
          Title?&lt;br&gt;
              &lt;input type="text" name="title"&gt;&lt;br&gt;
          Image-link&lt;br&gt;
              &lt;input type="img" name="imagePath"&gt;&lt;br&gt;
          Quantity?&lt;br&gt;
              &lt;input type="number" name="quantity"&gt;&lt;br&gt;&lt;br&gt;
              &lt;button type="submit"&gt;Add to list&lt;/button&gt;
          &lt;/form&gt;
          &lt;/html&gt;
          &lt;/body&gt;`;
           return html;
  ´´´
  </code></pre>