To handle initial connection errors, you should use.catch()ortry/catchwith async/await. mongoose.connect('mongodb://127.0.0.1:27017/test').catch(error=>handleError(error));// Or:try{awaitmongoose.connect('mongodb://127.0.0.1:27017/test'); }catch(error) {handleError(error); } ...
mongoose.connect('mongodb://127.0.0.1:27017/test').catch(error=>handleError(error));// Or:try{awaitmongoose.connect('mongodb://127.0.0.1:27017/test'); }catch(error) {handleError(error); } To handle errors after initial connection was established, you should listen for error events on th...
const err = await mongoose.connect('mongodb://nota.domain:27017/test', options). catch(err => err); // 'failed to connect to server [nota.domain:27017] on first connect' err.message; 许多较早的教程建议收听连接事件。 这不是绝对必要的,因为自行处理自动重新如果 Mongoose 在初始连接后失去与...
Prerequisites I have written a descriptive issue title I have searched existing issues to ensure the bug has not already been reported Mongoose version 6.5.3 Node.js version v18.0.0 MongoDB server version 5.0.8 Description mongodb://loca...
Error connecting to MongoDB: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP ...
mongoose.connect('mongodb://localhost:27017/test?connectTimeoutMS=1000', { useMongoClient:true});// The above is equivalent to:mongoose.connect('mongodb://localhost:27017/test', { useMongoClient:true, connectTimeoutMS:1000}); The disadvantage of putting options in the query string is that...
To handle initial connection errors, you should use.catch()ortry/catchwith async/await. mongoose.connect('mongodb://127.0.0.1:27017/test').catch(error=>handleError(error));// Or:try{awaitmongoose.connect('mongodb://127.0.0.1:27017/test'); }catch(error) {handleError(error); } ...