Getting an error when running node js and mongodb. Working fine but getting an error in the console

Viewed 22

I am getting an error like this

    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)

Here is my code

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",function(err){
  if(err)
    console.log("Here is the error");
  else
    console.log("Successfully connected to MongoDB");
});

const itemsSchema  = new mongoose.Schema({
  name:String
});

const Item = mongoose.model("Item",itemsSchema);

const item1 = new Item({
  name: "Complete this course"
});

const item2 = new Item({
  name: "Complete RPA course"
});

const item3 = new Item({
  name: "Learn Programming"
});

const defaultArray = [item1, item2, item3];

app.get("/", function(req, res) {
  let check=false;
  Item.find(function(err,item){
    if(err)
      console.log("Error in finding ");
    else{
      if(item.length==0){
        Item.insertMany(defaultArray,function(err){
          if(err)
            console.log("Error in inserting ");
          else
            console.log("Successfully saved");
        });
        if(check==false){
          res.redirect("/");
          check=true;
        }
      }
      res.render("list", {listTitle: "Today", newListItems: item});
    }
  })

});

app.post("/", function(req, res){
  const itemName = req.body.newItem;
  if(itemName!=""){
    const item = new Item({
      name: itemName,
    });
    item.save();
  }
  res.redirect("/");
});

app.get("/work", function(req,res){
  res.render("list", {listTitle: "Work List", newListItems: workItems});
});

app.get("/about", function(req, res){
  res.render("about");
});

app.listen(3000, function() {
  console.log("Server started on port 3000");
});

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

0 Answers
Related