Mongoose が自分の DB に接続できない場合、エラー処理のコールバックを設定するにはどうすればよいですか?
私は知っている
connection.on('open', function () { ... });
しかし、次のようなものはありますか
connection.on('error', function (err) { ... });
?
Mongoose が自分の DB に接続できない場合、エラー処理のコールバックを設定するにはどうすればよいですか?
私は知っている
connection.on('open', function () { ... });
しかし、次のようなものはありますか
connection.on('error', function (err) { ... });
?
接続すると、コールバックでエラーを取得できます。
mongoose.connect('mongodb://localhost/dbname', function(err) {
if (err) throw err;
});
使用できるマングース コールバックは多数ありますが、
// 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);
});
});
詳細: http://theholmesoffice.com/mongoose-connection-best-practice/
遅い回答ですが、サーバーの実行を維持したい場合は、これを使用できます:
mongoose.connect('mongodb://localhost/dbname',function(err) {
if (err)
return console.error(err);
});