0

I was wondering what I'm doing wrong here.I couldn't connect to the server and gives me a message that a promise was rejected and was not handled by catch(). Am i missing an async function somewhere? Thanks in advance.

    const mongoose = require("mongoose");

const Dishes = require("./models/dishes");

const url = "mongodb://localhost:27017/conFusion";
const connect = mongoose.connect(url);

mongoose.set("useUnifiedTopology", true);
mongoose.set("useNewUrlParser", true);

connect.then(db => {
  console.log("Connected correctly to server");

  var newDish = Dishes({
    name: "Uthappizza",
    description: "test"
  });

  newDish
    .save()
    .then(dish => {
      console.log(dish);

      return Dishes.find({});
    })
    .then(dishes => {
      console.log(dishes);

      return Dishes.remove({});
    })
    .then(() => {
      return mongoose.connection.close();
    })
    .catch(err => {
      console.log(err);
    });
});
Thomas
  • 11
  • 2
  • You have no rejection handler on the promise from `mongoose.connect` itself (your `connect` constant). you only use `then`, not `catch`, and you don't supply the second argument to `then`. – T.J. Crowder Jan 22 '21 at 10:08
  • The `catch()` you have, only catches rejects that happen in the promise chain starting at `newDish` but you never even reach that, because `connect` already throws. – derpirscher Jan 22 '21 at 10:09

1 Answers1

0

You have no rejection handler on the promise from mongoose.connect itself (your connect constant). you only use then, not catch, and you don't supply the second argument to then.

So the minimum change is:

connect.then(db => {
    // ...
})
.catch(error => {                            // ***
    // ...handle/report error connecting...  // ***
});                                          // ***

Am i missing an async function somewhere?

No, but using one might make the code easier to follow (this is subjective). The purpose of async/await is to make it possible to write code with our usual flow-control structures while using promises.

For instance, if you can't use top-level await or you don't want to, you could wrap all your code in an immediately-invoked async function like this:

const mongoose = require("mongoose");
const Dishes = require("./models/dishes");

const url = "mongodb://localhost:27017/conFusion";

// If you can't use top level `await`, you can use an `async` IIFE
(async () => {
    const connect = await mongoose.connect(url);

    mongoose.set("useUnifiedTopology", true);
    mongoose.set("useNewUrlParser", true);

    console.log("Connected correctly to server");

    var newDish = Dishes({
        name: "Uthappizza",
        description: "test"
    });

    const dish = await newDish.save();
    console.log(dish);
    const dishes = await Dishes.find({});
    console.log(dishes);

    await Dishes.remove({});
    await mongoose.connection.close();
})().catch(error => {
    // ...handle/report error...
});

If you can use top-level await and you want to (it does mean you have to switch to using JavaScript's own module syntax instead of CJS modules, but IMHO that's a good thing anyway), you can do this:

import mongoose from "mongoose";
import Dishes from "./models/dishes.js";

const url = "mongodb://localhost:27017/conFusion";

try {
    const connect = await mongoose.connect(url);

    mongoose.set("useUnifiedTopology", true);
    mongoose.set("useNewUrlParser", true);

    console.log("Connected correctly to server");

    var newDish = Dishes({
        name: "Uthappizza",
        description: "test"
    });

    const dish = await newDish.save();
    console.log(dish);
    const dishes = await Dishes.find({});
    console.log(dishes);

    await Dishes.remove({});
    await mongoose.connection.close();
} catch (error) {
    // ...handle/report error...
}

Note, though, that you'd probably only want to do that in your top-level module (which it looks like this is), since it holds up resolution of the module tree. That's fine for the entry point module, or a module that can't create its exports until a promise settles, but for the logic in the code you've shown it would probably only be appropriate in the entry point module.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875