我将connect-mongo用于我的 ExpressJS 会话存储和Mongoose用于我的数据库连接,并且我将称之为时间问题。这是我的代码:
dbservice.js
const mongoose = require('mongoose');
const debug = require('debug')('app:core:db:service');
const chalk = require('chalk');
const config = require('config');
const mongoDbUrl = config.mongodb_url;
const mongoDbOptions = {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
};
(async () => {
try {
await mongoose.connect(mongoDbUrl, mongoDbOptions);
debug(`Connected to ${chalk.green('MongoDB')}`);
} catch (err) {
debug(`${chalk.green('MongoDB')} connection ${chalk.red(`error`)}: ${err}`);
}
})();
const db = mongoose.connection;
db.on('error', (err) => {
debug(`${chalk.green('MongoDB')} connection ${chalk.red(`error`)}: ${err}`);
});
// For nodemon restarts
process.once('SIGUSR2', () => {
const msg = 'Nodemon Restart';
db.close()
.then(() => {
debug(`${chalk.green('MongoDB')} connection ${chalk.red(`closed`)}: ${msg}`);
process.kill(process.pid, 'SIGUSR2');
})
.catch((err) => {
debug(err);
});
});
const dbClient = db.getClient();
module.exports = dbClient;
app.js
const dbClient = require('dbService');
// Using Express Session with Google Cloud Datastore to securly store session/cookie information
app.use(
session({
// Using MongoDB as session store
store: MongoStore.create({
client: dbClient, // Use Mongoose for DB Conection
ttl: 1800000, // Ages session out at 30 minutes
autoRemove: 'native', // Uses MongoDB's native Time to Live (TTL) to expire and remove records
},
}),
}),
);
此代码在使用本地网络上的 MongoDB 实例时效果很好,因为连接到 MongoDB 的延迟非常小,它可以工作。但是,使用 MongoDB Atlas 时,连接会有一到两秒的延迟。所以,我怀疑(我认为很棒)我的代码行const dbClient = db.getClient()
失败dbservice.js
并connect-mongo
抛出错误:
(node:1148) UnhandledPromiseRejectionWarning: MongoError: MongoClient must be connected before calling MongoClient.prototype.db` because Mongoose is not connected.
作为一个新手,我一直在努力尝试解决这个问题。我试图找到一种方法来mongoose.connection.readyState
等待它返回一个1
for connected 但失败了。
我的问题是:有没有更好的方法让我正确地返回dbClient
它以等待 Mongoose 连接到 MongoDB?任何帮助是极大的赞赏。