is there a mongoose connect error callback

后端 未结 6 1402
别那么骄傲
别那么骄傲 2020-11-30 02:42

how can i set a callback for the error handling if mongoose isn\'t able to connect to my DB?

i know of

connection.on(\'open\', function () { ... });
         


        
6条回答
  •  暖寄归人
    2020-11-30 03:20

    • Handle (catch) the connect exceptions
    • Handle other connection errors
    • show a message when successfully connected
    mongoose.connect(
      "mongodb://..."
    ).catch((e) => {
      console.log("error connecting to mongoose!");
    });
    mongoose.connection.on("error", (e) => {
      console.log("mongo connect error!");
    });
    mongoose.connection.on("connected", () => {
      console.log("connected to mongo");
    });
    

提交回复
热议问题