title: TypeError: Cannot read property 'items' of null in monogdb using nodejs
body: <p>I am creating a todo list app , which adds items dynamically and creates list dynamically through url. For example : <a href="http://localhost:3000/Home" rel="nofollow noreferrer">http://localhost:3000/Home</a> will create a "Home" list and <a href="http://localhost:3000/Work" rel="nofollow noreferrer">http://localhost:3000/Work</a> will create "Work" with some default values pre-added. But when i go through url like : <a href="http://localhost:3000/Home" rel="nofollow noreferrer">http://localhost:3000/Home</a> and add new item , then it throws an error. </p>
  
  <p><strong>Below is my code for app.js</strong></p>
  
  <pre><code>//jshint esversion:6
  
  const express = require("express");
  const bodyParser = require("body-parser");
  const mongoose=require("mongoose");
  
  const app = express();
  
  app.set('view engine', 'ejs');
  
  app.use(bodyParser.urlencoded({extended: true}));
  app.use(express.static("public"));
  
  mongoose.connect("mongodb://localhost:27017/todolistDB",{ useNewUrlParser:true , useUnifiedTopology: true });
  const itemsSchema={
    name:String
  };
  
  const Item=mongoose.model("Item",itemsSchema);
  
  const item1=new Item({
    name:"Welcome to your todolist"
  });
  
  const item2=new Item({
    name:"Hit the + button to add new item"
  });
  
  const item3=new Item({
    name:"&lt;-- Hit this to delete an item"
  });
  
  const defaultItems=[item1,item2,item3];
  
  const listSchema={
    name:String,
    items:[itemsSchema]
  };
  
  const List=mongoose.model("List",listSchema);
  
  
  
  app.get("/", function(req, res) {
  
    // finding elements using mongoose and to check if array is empty then add items.
    // if array already contains items then array does not add same items again when starting server mupltipe times.
  
    Item.find({},function(err,foundItems){
      if (foundItems.length===0) {
        Item.insertMany(defaultItems,function(err){
          if (err) {
            console.log(err);
          } else {
            console.log("Saved Successfully.....");
          }
        });
        res.redirect("/");
      } else {
        res.render("list", {listTitle: "Today", newListItems: foundItems});
      }
    });
  
  
  
  
  });
  
  app.post("/", function(req, res){
  
    const itemName = req.body.newItem;
    const listName=req.body.list;
  
  
    const item= new Item({
      name:itemName
    });
  
    if(listName==="Today"){
      item.save();
      res.redirect("/");
    }
    else{
      List.findOne({name:listName},function(err,foundList){
        foundList.items.push(item); **// NODE SHELL SHOWING ERROR IN THIS LINE**
        foundList.save();
        res.redirect("/"+listName);
      });
    }
  
  
  });
  
  app.post("/delete",function(req,res){
    const checkedItemId=(req.body.checkbox);
    Item.findByIdAndRemove(checkedItemId,function(err){
      if (err) {
        console.log(err);
      } else {
        console.log("Selected item deleted successfully");
        res.redirect("/");
      }
    });
  });
  
  // app.get("/work", function(req,res){
  //   res.render("list", {listTitle: "Work List", newListItems: workItems});
  // });
  
  app.get("/:customListName",function(req,res){
    const customListName=req.params.customListName;
    console.log(customListName);
    List.findOne({name:customListName},function(err,foundList){
      if(!err){
        if(!foundList){
          // create a new list
          const list=new List({
            name:customListName,
            items: defaultItems
          });
          list.save();
          res.redirect("/"+customListName);
        }
        else{
          // show an existing list
          res.render("list",{listTitle:foundList.name,newListItems:foundList.items});
        }
      }
      });
  
  
  });
  
  app.get("/about", function(req, res){
    res.render("about");
  });
  
  app.listen(3000, function() {
    console.log("Server started on port 3000");
  });
  </code></pre>
  
  <p><strong>Code for list.ejs</strong></p>
  
  <pre><code>&lt;%- include("header") -%&gt;
  
    &lt;div class="box" id="heading"&gt;
      &lt;h1&gt; &lt;%= listTitle %&gt; &lt;/h1&gt;
    &lt;/div&gt;
  
    &lt;div class="box"&gt;
      &lt;% newListItems.forEach(function(itemName){ %&gt;
        &lt;form action="/delete" method="post"&gt;
  
  
        &lt;div class="item"&gt;
          &lt;input type="checkbox" name="checkbox" value="&lt;%= itemName.id %&gt;" onChange="this.form.submit()"&gt;
          &lt;p&gt;&lt;%=  itemName.name  %&gt;&lt;/p&gt;
        &lt;/div&gt;
        &lt;/form&gt;
        &lt;% }) %&gt;
  
        &lt;form class="item" action="/" method="post"&gt;
          &lt;input type="text" name="newItem" placeholder="New Item" autocomplete="off"&gt;
          &lt;button type="submit" name="list" value="&lt;%= listTitle %&gt; "&gt;+&lt;/button&gt;
        &lt;/form&gt;
    &lt;/div&gt;
  
  &lt;%- include("footer") -%&gt;
  </code></pre>
  
  <p><strong>Any solution please ?</strong></p>