title: Getting an error when running node js and mongodb. Working fine but getting an error in the console
body: <p>I am getting an error like this</p>
  <pre><code>    at new NodeError (node:internal/errors:387:5)
      at ServerResponse.setHeader (node:_http_outgoing:603:11)
      at ServerResponse.header (D:\web development\todolist-v2-starting-files\node_modules\express\lib\response.js:767:10)     
      at ServerResponse.send (D:\web development\todolist-v2-starting-files\node_modules\express\lib\response.js:170:12)       
      at done (D:\web development\todolist-v2-starting-files\node_modules\express\lib\response.js:1004:10)
      at tryHandleCache (D:\web development\todolist-v2-starting-files\node_modules\ejs\lib\ejs.js:257:5)
      at View.exports.renderFile [as engine] (D:\web development\todolist-v2-starting-files\node_modules\ejs\lib\ejs.js:482:10)    at View.render (D:\web development\todolist-v2-starting-files\node_modules\express\lib\view.js:135:8)
      at tryRender (D:\web development\todolist-v2-starting-files\node_modules\express\lib\application.js:640:10)
      at Function.render (D:\web development\todolist-v2-starting-files\node_modules\express\lib\application.js:592:3)
  </code></pre>
  <p>Here is my code</p>
  <pre><code>const express = require(&quot;express&quot;);
  const bodyParser = require(&quot;body-parser&quot;);
  const mongoose = require(&quot;mongoose&quot;);
  
  const app = express();
  
  app.set('view engine', 'ejs');
  
  app.use(bodyParser.urlencoded({extended: true}));
  app.use(express.static(&quot;public&quot;));
  
  mongoose.connect(&quot;mongodb://localhost:27017/toDoListDB&quot;,function(err){
    if(err)
      console.log(&quot;Here is the error&quot;);
    else
      console.log(&quot;Successfully connected to MongoDB&quot;);
  });
  
  const itemsSchema  = new mongoose.Schema({
    name:String
  });
  
  const Item = mongoose.model(&quot;Item&quot;,itemsSchema);
  
  const item1 = new Item({
    name: &quot;Complete this course&quot;
  });
  
  const item2 = new Item({
    name: &quot;Complete RPA course&quot;
  });
  
  const item3 = new Item({
    name: &quot;Learn Programming&quot;
  });
  
  const defaultArray = [item1, item2, item3];
  
  app.get(&quot;/&quot;, function(req, res) {
    let check=false;
    Item.find(function(err,item){
      if(err)
        console.log(&quot;Error in finding &quot;);
      else{
        if(item.length==0){
          Item.insertMany(defaultArray,function(err){
            if(err)
              console.log(&quot;Error in inserting &quot;);
            else
              console.log(&quot;Successfully saved&quot;);
          });
          if(check==false){
            res.redirect(&quot;/&quot;);
            check=true;
          }
        }
        res.render(&quot;list&quot;, {listTitle: &quot;Today&quot;, newListItems: item});
      }
    })
  
  });
  
  app.post(&quot;/&quot;, function(req, res){
    const itemName = req.body.newItem;
    if(itemName!=&quot;&quot;){
      const item = new Item({
        name: itemName,
      });
      item.save();
    }
    res.redirect(&quot;/&quot;);
  });
  
  app.get(&quot;/work&quot;, function(req,res){
    res.render(&quot;list&quot;, {listTitle: &quot;Work List&quot;, newListItems: workItems});
  });
  
  app.get(&quot;/about&quot;, function(req, res){
    res.render(&quot;about&quot;);
  });
  
  app.listen(3000, function() {
    console.log(&quot;Server started on port 3000&quot;);
  });
  </code></pre>
  <p>Here the thing is I am getting the correct output on the web page, but in the console, there was an error. I am trying to handle that but getting the same error, no matter how I tried. Can somebody tell me why I am getting an error like this! I already included check variable in order to handle this error, but didn't work. Any help would be appreciated.
  Thanks in Advance</p>