skip to Main Content

Good morning. I hope You are well.So i try finish some udemy course but i stuck with this code because mogoose no longer accepts callback function.I tried to change function to :"then"+".catch", but still not working.

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

and error form terminal:
"MongooseError: Model.find() no longer accepts a callback"

Sorry to take up your time, I know you have better things to do.

3

Answers


  1. Instead of passing a callback, Monggose methods are now returning Promises. So you should go for something like this

    app.get("/", function (req, res) {
      const foundItems = await Item.find({});
      if (foundItems.length === 0) {
          Item.insertMany(defaultItems).then(() => 
            console.log("Successfully saved default items to DB."))
            .catch((err) =>  console.log(err));
          res.redirect("/");
        } else {
          res.render("list", { listTitle: "Today", newListItems: 
          foundItems });
        }
    });
    
    Login or Signup to reply.
  2. I’m new here..sorry if not a good enough answer…but

    You’re gonna have to use the async/await focntionnality…

    You have to enclose :

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

    in a async fonction… and call it with await.

    Here is an example of my code :

        
    export async function getPublicBottles(): Promise<bottle[]> {
        
        const bottles = await Bottle.find({userTargetName:"PublicBottle"});
        if (!bottles) {
            console.warn("[DATA] No users in database.");
            throw createError(httpStatus.NOT_FOUND, "No bottles found");
        }
    
        return bottles;
    }

    then you call :

    app.get("/", function (req, res) {
      const data = await getPublicBottles();
    });
    Login or Signup to reply.
  3. Heyy, i’m taking the same course and that’s how i did it :

    app.get("/", function (req, res) {
    
        async function myitems() {
            const foundItems = await Item.find({});
            
            if (foundItems.length === 0) {
            
                Item.insertMany(defaultItems);
                res.redirect("/");
                
            } else {
                res.render("list", { listTitle: "Today", listItems: foundItems });
            }
        }
        myitems();
    });

    in newer version of moongose a couple of things are different.
    await MyModel.find({}); needs to be inside a async function in order to work I called it myitems() like in the snippet above.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search