【发布时间】:2019-08-22 19:58:21
【问题描述】:
我已经在 docker 容器中启动并运行了 mongodb。我停止容器,节点返回 MongoError。我重新启动容器,节点继续抛出相同的 MongoError。
我希望它在出现问题时重新连接。
const uri: string = this.config.db.uri;
const options = {
useNewUrlParser: true,
useCreateIndex: true,
autoIndex: true,
autoReconnect: true,
},
mongoose.connect(uri, options).then(
() => {
this.log.info("MongoDB Successfully Connected On: " + this.config.db.uri);
},
(err: any) => {
this.log.error("MongoDB Error:", err);
this.log.info("%s MongoDB connection error. Please make sure MongoDB is running.");
throw err;
},
);
我如何设置 mongoose 在 mongodb 连接失败时尝试自动连接。
【问题讨论】: