Javascript 是否有猫鼬连接错误回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6676499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there a mongoose connect error callback
提问by pkyeck
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 () { ... });
but is there something like
但有没有像
connection.on('error', function (err) { ... });
?
?
回答by evilcelery
When you connect you can pick up the error in the callback:
当您连接时,您可以在回调中获取错误:
mongoose.connect('mongodb://localhost/dbname', function(err) {
if (err) throw err;
});
回答by thisarattr
there many mongoose callback you can use,
您可以使用许多猫鼬回调,
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + dbURI);
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
more on: http://theholmesoffice.com/mongoose-connection-best-practice/
更多信息:http: //theholmesoffice.com/mongoose-connection-best-practice/
回答by Asta
In case anyone happens upon this, the version of Mongoose I'm running (3.4) works as stated in the question. So the following can return an error.
如果有人遇到这种情况,我正在运行的 Mongoose 版本 (3.4) 会按照问题中的说明工作。因此,以下内容可能会返回错误。
connection.on('error', function (err) { ... });
回答by valdeci
As we can see on the moongose documentation for Error Handling, since the connect()method returns a Promise, the promise catch
is the option to use with a mongoose connection.
正如我们在错误处理的 Moongose 文档中看到的那样,由于connect()方法返回一个 Promise,promisecatch
是与 mongoose 连接一起使用的选项。
So, to handle initial connection errors, you should use .catch()
or try/catch
with async/await
.
因此,要处理的初始连接错误,你应该使用.catch()
或try/catch
使用async/await
。
In this way, we have two options:
这样,我们有两个选择:
Using the .catch()
method:
使用.catch()
方法:
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true })
.catch(error => console.error(error));
or using try/catch:
或使用 try/catch:
try {
await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
} catch (error) {
console.error(error);
}
IMHO, I think that using catch
is the cleaner way.
恕我直言,我认为使用catch
是更清洁的方式。
回答by vcanales
Late answer, but if you want to keep the server running you can use this:
迟到的答案,但如果你想保持服务器运行,你可以使用这个:
mongoose.connect('mongodb://localhost/dbname',function(err) {
if (err)
return console.error(err);
});