1

Here is my full app.js code.

//jshint esversion:6 

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const _ = require("lodash");

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});

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 a new item."
});

const item3 = new Item({
  name: "<-- 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) {
  Item.find({}, function(err, foundItems){

      if (foundItems.length === 0) {
        Item.insertMany(defaultItems, function(err){
          if (err) {
            console.log(err);
          } else {
            console.log("Successfully savevd default items to DB.");
          }
        });
        res.redirect("/");
      } else {
        res.render("list", {listTitle: "Today", newListItems: foundItems});
      }
  });
});

app.get("/:customListName", function(req, res){
  const customListName = _.capitalize(req.params.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.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);
      foundList.save();
      res.redirect("/" + listName);
    });
  }
});

app.post("/delete", function(req, res){
  const checkedItemId = req.body.checkbox;
  const listName = req.body.listName;

  if (listName === "Today") {
    Item.findByIdAndRemove(checkedItemId, function(err){
      if (!err) {
        console.log("Successfully deleted checked item.");
        res.redirect("/");
      }
    });
  } else {
    List.findOneAndUpdate({name: listName}, {$pull: {items: {_id: checkedItemId}}}, function(err, foundList){
      if (!err){
        res.redirect("/" + listName);
      }
    });
  }
});

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

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

Cannot understand where the error is!

The error is:

Model.find() no longer accept a call back.
FiddlingAway
  • 1,598
  • 3
  • 14
  • 30

3 Answers3

1

The error

MongooseError: Model.find() no longer accepts a callback

occurs because starting from Mongoose version 6, the find() method no longer accepts a callback function as the second argument. Instead, it returns a promise that you can handle using .then() or async/await syntax.

To fix the error, you can modify your code to use promises instead of callbacks.

Here's the updated code:

app.get("/", function(req, res){
   Item.find({})
      .then(foundItems => {
          res.render("list", {listTitle: "Today", newListItem: foundItems});
    })
      .catch(err => {
          console.log(err);
  });

});

In the updated code, Item.find({}) returns a promise, and you can use the .then() method to handle the resolved value, which in this case is the foundItems array. If an error occurs during the query execution, you can handle it using the .catch() method.

Make sure you have a compatible version of Mongoose installed in your project. If you're using an older version of Mongoose (pre-6.0), you can continue using the callback syntax without modifying your code.

Mahesh
  • 11
  • 2
0

You can use an async function instead:

app.get("/", async (req, res) => {
const allItems = await Item.find();
console.log("Found " + allItems.length + " numbers of items in the database.");
});
user3626720
  • 105
  • 1
  • 9
-1

The error message is self-explanatory: A callback function is a function that you pass as an argument to another function, and that will be called/executed later on after a certain event.

In your specific case, the find method doesn't accept anymore callback functions so you need to remove function(err, foundItems)... from Item.find:

Item.find({}, function(err, foundItems){
  // Code
});

For more info about it, kindly check this link to the docs

underflow
  • 1,545
  • 1
  • 5
  • 19