catch νΈλ€λ¬μμ mongoose.connect()
μ€λ₯λ₯Ό μ²λ¦¬νλ λ°©λ²μ 무μμ
λκΉ? μμ© νλ‘κ·Έλ¨ μ΄κΈ°ν 체μΈμ μ¬μ©νκ³ μΆμ§λ§ mongoose.connect()
κ° κ±°λΆλ μ½μμ λ°ννμ§ μκΈ° λλ¬Έμ κ·Έλ κ² ν μ μμ΅λλ€. μ½λ°±μ μ§μ νλ κ²½μ°μλ§ κ±°λΆλ μ½μμ λ°ννμ§λ§ μλ²½ν μ루μ
μ μλλλ€.
μμ:
mongoose.connect('mongodb://127.0.0.2/test') // if error it will throw async error
.then(() => { // if all is ok we will be here
return server.start();
})
.catch(err => { // we will not be here...
console.error('App starting error:', err.stack);
process.exit(1);
});
ν΄κ²° λ°©λ²:
mongoose.connect('mongodb://127.0.0.2/test', function() { /* dummy function */ })
.then(() => {
return server.start();
})
.catch(err => { // mongoose connection error will be handled here
console.error('App starting error:', err.stack);
process.exit(1);
});
mongoose.connect()
λ μ΄μ λ²μ κ³Όμ νΈνμ±μ κΉ¨λ¨λ¦¬μ§ μκΈ° μν΄ κ±°λΆλ μ½μμ λ°ννλ λμ λΉλκΈ° μ€λ₯λ₯Ό λμ‘λ€κ³ μκ°ν©λλ€. μ¬μ©μλ λͺ½κ΅¬μ€ μ°κ²° μ€μ μ λ¬Έμ κ° μλ κ²½μ° μμ© νλ‘κ·Έλ¨μ΄ μ€λ₯ μ½λμ ν¨κ» μλ£λ κ²μΌλ‘ κΈ°λν©λλ€. mongoose.connect()
κ° κ±°λΆλ μ½μμ λ°ννλ©΄ μ½λ 0μΌλ‘ μλ£λκ³ μ½μμ μ무 κ²λ μΆλ ₯λμ§ μμ΅λλ€. λ°λΌμ μ½μμ λ°ννκΈ° μν΄ mongoose.connect()
λΌκ³ λ§νλ λ°©λ²μ΄ μμΌλ©΄ μ’μ κ²μ
λλ€. exec()
μ κ°μ κ²μΌ μ μμ΅λλ€.
mongoose.connect('mongodb://127.0.0.2/test').exec()
.then(() => { // if all is ok we will be here
return server.start();
})
.catch(err => { // if error we will be here
console.error('App starting error:', err.stack);
process.exit(1);
});
mongoose.connect('mongodb://localhost/dbCollection', function(err, db) {
if (err) {
console.log('Unable to connect to the server. Please start the server. Error:', err);
} else {
console.log('Connected to Server successfully!');
}
});
@nasr18 무μ¨
@Jokero μ£μ‘ν©λλ€
mongoose.connectμ μ½λ°±μ μ¬μ©νμ¬ μ°κ²° μ€ μ€λ₯λ₯Ό ν¬μ°©νμμμ€.
μ΄λ²€νΈκ° μ΄λ¦¬λ©΄ μλ²λ₯Ό μμν μ μμ΅λλ€.
mongoose.connection.once('open', function() {
logger.info('MongoDB event open');
logger.debug('MongoDB connected [%s]', url);
mongoose.connection.on('connected', function() {
logger.info('MongoDB event connected');
});
mongoose.connection.on('disconnected', function() {
logger.warn('MongoDB event disconnected');
});
mongoose.connection.on('reconnected', function() {
logger.info('MongoDB event reconnected');
});
mongoose.connection.on('error', function(err) {
logger.error('MongoDB event error: ' + err);
});
// return resolve();
return server.start();
});
return mongoose.connect(url, options, function(err) {
if (err) {
logger.error('MongoDB connection error: ' + err);
// return reject(err);
process.exit(1);
}
});
@mathieug λλ μ€λ₯ μ²λ¦¬λ₯Ό μν΄ μ½λ°±μ μ¬μ©ν μ μλ€λ κ²μ μκ³ μμ§λ§ λ¬Έμ λ μ½μμ κ΄ν κ²μ λλ€.
mongoose.connectμ μ½μμ μ¬μ©νμ§ λ§κ³ μ°κ²°μ΄ μ²μ μ΄λ¦΄ λ μλ²λ₯Ό μμνμμμ€(.once('open'))
μμ? μ ν μ°¨μ΄κ° μμ΅λλ€. μ°κ²°μ΄ μ΄λ¦¬λ©΄ mongoose.connect
μ½λ°±μ΄ νΈμΆλκ³ λ°νλ promiseκ° μ΄νλκ³ open
μ΄λ²€νΈκ° λ°μν©λλ€. μνλ κ²μ μ¬μ©ν μ μλλ‘
λ€, νμ§λ§ λ§μνμ λλ‘ mongoose.connectλ μ€λ₯κ° λ°μν κ²½μ° μ½λ°±μ΄ νμν©λλ€.
μ΄κ²μ΄ μ κ° μ΄ λ¬Έμ λ₯Ό λ§λ μ΄μ μ λλ€ :μ€λ§μΌλ¦¬:
κ°μ λ¬Έμ κ° μμ΅λλ€.
mongoose.connect(...).catch(failCallback)
μ(λ₯Ό) μ¬μ©νκ³ μΆμ§λ§ μ΄κΈ° μ°κ²° μλ μ μ€λ₯κ° λ°μνλ©΄ failCallback
κ° μ€νλμ§ μμ΅λλ€. mongoose.connect
λ°ννλ MongooseThenable
μμ¬ μ½μμ λ¬Έμ κ° μμ΅λλ€. κ²λ€κ° μ λ mongoose.Promise = require('bluebird')
νκ³ κ·Έλ¬ν λΉλκΈ° νΈμΆμ΄ μ κ²½μ°μλ Bluebird μ½μμΈ _real_ μ½μμ λ°νν κ²μΌλ‘ μ λ§λ‘ κΈ°λν©λλ€.
κ·Έλ¬λ λ΄ failCallback
κ° μ€νλμ§ μλ λ¬Έμ λ μ²μμλ§ λ°μν©λλ€. μ:
// First kill mongod instance so that Mongoose cannot connect and start the app
mongoose.connect(uri, options).catch(failCallback); // failCallback does _not_ get called
// Then stop the app, start mongod instance so that Mongoose can connect, start the app
mongoose.connect(uri, options).then(successCallback); // successCallback gets called
// Then kill mongod instance so that Mongoose disconnects (while the app is running)
mongoose.connection.on('disconnected', function () {
setTimeout(function () {
mongoose.connect(uri, options).catch(reconnectFailCallback); // now reconnectFailCallback _does_ get called
}, randomDelayOfAboutASecond);
});
κ·Έλ¬λ μ€μ λ‘ λ€μκ³Ό κ°μ΄ μλν©λλ€(무μλ―Έν©λλ€).
// First kill mongod instance so that Mongoose cannot connect and start the app
var dummyNoopCallback = function () {};
mongoose.connect(uri, options, dummyNoopCallback).catch(failCallback); // failCallback _does_ get called
μ κ·Έλ° κ²λκΉ?
Re: μ€μ μ½μμ λ°ννλ κ²μ μ΄μ λ²μ κ³Όμ νΈνμ±μ μν λ°©λ²μ
λλ€. λΆννλ μ¬λλ€μ μ΄λ€ μ΄μ λ‘(#3847, #3790 λ±) .connect()
μ°κ²° λμμ μμ‘΄νλ―λ‘ μ§κΈμ μ΄ λ°©μμ μ μ§ν©λλ€ :) .catch()
κ° μ΄κΈ° μ°κ²°μ μ‘μ μ μλλ‘ μμ μ¬νμ μΆκ°νμ΅λλ€. λ¬Έμ .
ν¨κ³Όκ°μλ€! κ·Έλ¬λ μ΄λ¬ν λ³κ²½ μ¬νμ΄ μ°κ²° μ€λ₯κ° λ°μνκ³ μ½λ°± λ° μ½μ μ²λ¦¬κΈ°κ° μμ λ μμ© νλ‘κ·Έλ¨ νλ‘μΈμ€κ° μλ£λ κ²μΌλ‘ μμνλ μ¬λλ€μκ² λ¬Έμ κ° λ κΉμ?
μμ:
const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect('mongodb://127.0.0.2/test'); // no callback and promise handlers
μ΄λ¬ν λ³κ²½ μ μ
/home/dmitry/example/node_modules/mongodb/lib/server.js:242
process.nextTick(function() { throw err; })
^
Error: connect ECONNREFUSED 127.0.0.2:27017
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14)
μ΄λ¬ν λ³κ²½μΌλ‘ νλ‘μΈμ€λ 0 μ½λλ‘ μλ£λ©λλ€.
μ μ κ°μ¬ν©λλ€. μ‘°μ¬νκ² μ΅λλ€.
μμν! κ°μ¬ν©λλ€:+1:
+1
μ΄κΈ°ν 체μΈμμ .connect()μ κ²°κ³Όλ₯Ό μμ ν Promiseλ‘ λ°νν μ μκΈ°λ₯Ό μν©λλ€.
@CodeJjang μ΄ μ΄λ―Έ μλνμ§ μμ΅λκΉ? μ΄ κΈ°λ₯μ μ΄λ―Έ μΆμλμμ΅λλ€.
mongoose.connect('mongodb://127.0.0.2/test', function() { /* dummy function */ })
.then(() => {
return server.start();
})
.catch(err => { // mongoose connection error will be handled here
console.error('App starting error:', err.stack);
process.exit(1);
});
μ΄κ²μ λλ₯Ό μν΄ μλνμ§ μμ΅λλ€ :(
κ·Έκ²μ 보μ¬
(node:22564) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: failed to connect to server [127.0.0.1:27017] on first connect
ν mongoose.connect()
μμ μ½λ°±μ μ κ±°ν΄ λ³΄μΈμ.
λ°©κΈ μ»μμ΅λλ€:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1):
MongoError: failed to connect to server [localhost:17551] on first connect
mongoose 4.7.6(νμ¬ μ΅μ λ²μ )μμ μ½μμ μ¬μ©νμ§ μλ κ²½μ° κ²½κ³ λ₯Ό νΌν μ μλ λ°©λ²μ΄ μμ΅λκΉ?
ExpressJSμ ν¨κ» μ¬μ©νκ³ μλλ° catchλ₯Ό μ¬μ©νλ©΄ express-session
κ° MongoStoreλ μμ±νλ―λ‘ μ΄μνκ² μλν©λλ€. κ·Έλ₯ μ§μ λΆν©λλ€.
μ½λ μν λΆνλ립λλ€
@matheoμ κ°μ λ¬Έμ κ° μμ΅λλ€. μ΄ ν°μΌμ λ°©κΈ μ΄μμ΅λλ€.
λμ€μ μ€λ μ¬λμ μν΄ νμΈνκΈ° μν΄ μμλλ‘ μλν©λλ€.
mongoose.connect('http://127.0.0.1:27017/test')
.then(() => {
server.start();
})
.catch((err) => {
console.log('Error on start: ' + err.stack);
process.exit(1);
});
λμ€μ μ€λ μ¬λμ μν΄ νμΈνκΈ° μν΄ μμλλ‘ μλν©λλ€.
mongoose.connect('http://127.0.0.1:27017/test') .then(() => { server.start(); }) .catch((err) => { console.log('Error on start: ' + err.stack); process.exit(1); });
μ λ μ΄ λ°©λ²μ μ¬μ©νκ³ μμ΅λλ€! κ·Έλ¬λ mongodbλ₯Ό μ€μ§ν λ€μ μ΄ μ½λλ₯Ό μ€ννλ©΄ μλνμ§ μμ΅λλ€.
// mongoose version 5.4.14
mongoose
.connect(databaseUri, {
promiseLibrary: bluebird,
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
// Automatically try to reconnect when it loses connection to MongoDB
autoReconnect: true,
// Never stop trying to reconnect
reconnectTries: Number.MAX_VALUE,
// Reconnect every 500ms
reconnectInterval: 500,
// Maintain up to 10 socket connections. If not connected,
// return errors immediately rather than waiting for reconnect
poolSize: 10,
// Give up initial connection after 10 seconds
connectTimeoutMS: 10000,
})
.catch((err) => {
console.log(err);
process.exit(1);
});
@nvtuan305 'μλνμ§ μμ'μ΄ λ¬΄μ¨ λ»μΈκ°μ? μ€λ₯ λ©μμ§κ° μμ΅λκΉ?
@vkarpov15 μ€λ₯κ° μΈμλμ§ μμμ μλ―Έν©λλ€.
μ μ°¨:
catch
λΈλ‘μ λλ¬νμ§ μμ΅λλ€. // Never reach to here
console.log(err);
process.exit(1);
@nvtuan305 μ΄κΈ° μ°κ²° μκ°μ΄ μ΄κ³Όλλ©΄ mongooseλ connectTimeoutMS
μ΄νμ μ€λ₯λ₯Ό λ³΄κ³ ν©λλ€. λΉμ μκ²λ κ·Έλ μ§ μμ΅λκΉ? κ·Έλ λ€λ©΄ MongoDBμ mongooseμ λ²μ μ 무μμ
λκΉ?
@vkarpov15
mongooseλ
connectTimeoutMS
νμ μ€λ₯λ₯Ό λ³΄κ³ ν©λλ€.
μ κ²½μ° λͺ½κ΅¬μ€λ catch
λΈλ‘μμ connectTimeoutMS
μ΄νμ μ€λ₯λ₯Ό λ³΄κ³ νμ§ μμ΅λλ€. μ λ Mongodb 3.6.5μ mongoose 5.4.14λ₯Ό μ¬μ©νκ³ μμ΅λλ€.
> mongod --version
db version v3.6.5
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
OpenSSL version: OpenSSL 1.0.2p 14 Aug 2018
allocator: system
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
@nvtuan305 νμΈμ μν΄ λ 립 μ€νν mongodb μΈμ€ν΄μ€ λλ 볡μ λ³Έ μΈνΈμ μ°κ²°νκ³ μμ΅λκΉ?
@vkarpov15 λ 립 μ€νν μΈμ€ν΄μ€λ₯Ό μ¬μ©νκ³ μμ΅λλ€.
@ nvtuan305 μ΄λ€ μ΄μ 체μ μ
λκΉ? λν Mongooseκ° λ
립 μ€νν μ°κ²°μ μ€ν¨ν κ²μΌλ‘ μμνλ μ΄μ λ 무μμ
λκΉ? mongod
μΈμ€ν΄μ€κ° λ€μ΄λμλμ§, λ€νΈμν¬ μ°κ²°μ΄ μλμ§, λ€λ₯Έ κ²μ΄ μμ΅λκΉ?
μλ μ€ν¬λ¦½νΈλ μμλλ‘ μλν©λλ€.
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
console.log(mongoose.version);
const start = Date.now();
const opts = { useNewUrlParser: true, connectTimeoutMS: 1000 };
await mongoose.connect('mongodb://doesntexist.com:27017/test', opts).
catch(error => { console.log(`Caught "${error.message}" after ${Date.now() - start}`); });
}
μ°μΆ
$ node gh-4135.js
5.4.14
Caught "failed to connect to server [doesntexist.com:27017] on first connect [MongoNetworkError: connection 0 to doesntexist.com:27017 timed out]" after 1024
done
$ ^C
μ΄μ΄, κΈ°λ³Έμ μΈ κ² κ°μ§λ§ μ΄ μ§μΉ¨μ λ€μ νμΈνμμμ€.
κ·Έλ κ² ν΄μ ν΄κ²°
κ°μ₯ μ μ©ν λκΈ
λμ€μ μ€λ μ¬λμ μν΄ νμΈνκΈ° μν΄ μμλλ‘ μλν©λλ€.