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); } ...
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); } ...
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 在初始连接后失去与...
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() or try/catch with async/await. mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true }). catch(error => handleError(error)); // Or: try { await mongoose.connect('mongodb://localhost:27017/test', { us...