[英]Mongoose Multi Connection with Fastify
我正在尝试通过 mongoose 在 fastify 中使用多个数据库连接。
对于单个数据库代码看起来像
const mongoose = require('mongoose');
const fastifyPlugin = require('fastify-plugin')
mongoose.Promise = Promise; // Set mongoose to use ES6 Promises.
const reconnectTimeout = 5000; // ms.
const db = mongoose.connection;
async function dbConnector1(fastify, options) {
const conn1 = mongoose.connect("mongodb://localhost/db1", {
useNewUrlParser: true,
useUnifiedTopology: true,
retryWrites: false
})
.then(() => console.log( 'Connection to DB1 successful'))
.catch((err) => console.error(err));
};
module.exports = fastifyPlugin(dbConnector1,{
name: 'DB1'
})
我已将上述代码放入 conn1.js 我在 conn2.js 和 index.js 中创建了与不同 db 的类似连接我正在注册 db 插件,如下所示
await fastify.register(require('./src/db/conn1'))
await fastify.register(require('./src/db/conn2'))
我收到以下错误 -
MongooseError:无法在具有不同连接字符串的活动连接上调用openUri()
。 确保您没有多次调用mongoose.connect()
。 请参阅: https://mongoosejs.com/docs/connections.html#multiple_connections
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.