I'm having trouble to connect Redis inside MongoClient.connect.
我在连接mongoclient中的Redis时遇到了麻烦。
Although both separately works, inside mongo s callback somehow Redis does not connect.
虽然两者都单独工作,但在mongo的回调中Redis并不连接。
My file structure is like this:
我的文件结构是这样的:
mongoUtil.js:
mongoUtil.js:
const chalk = require( 'chalk' )
const MongoClient = require( 'mongodb' ).MongoClient
const appConfig = require( '../config/appConfig' )
let _db
module.exports = {
connectToServer: callback => {
MongoClient.connect(
appConfig.MONGO_URL,
{ promiseLibrary: Promise },
( err, db ) => {
_db = db;
console.log( chalk.blue('Connected to MongoDB') )
callback( err )
}
)
},
getDb: function() {
return _db;
}
}
redisUtil.js:
redisUtil.js:
const chalk = require( 'chalk' )
const redis = require('redis')
let client = redis.createClient()
module.exports = {
connectToServer : callback => {
client.on('connect', () => {
console.log( chalk.red('Connected to Redis') )
callback()
} )
},
getDb: function() {
return client;
}
}
server.js: this does not work
服务器。这不行。
const mongoUtil = require( './lib/mongoUtil' )
const redisUtil = require( './lib/redisUtil' )
mongoUtil.connectToServer( err => {
redisUtil.connectToServer( () => {
// some server code
})
})
server.js: this works
服务器。js:这个作品
const mongoUtil = require( './lib/mongoUtil' )
const redisUtil = require( './lib/redisUtil' )
redisUtil.connectToServer( () => {
// some server code
})
mongoUtil.connectToServer( err => {
})
1 个解决方案
#1
2
I suspect this is a timing issue. By the time you call redisUtil.connectToServer
the connection to Redis will already have been established, so the connect
event won't fire. Calling redis.createClient()
will attempt to connect straight away, it won't wait for you to register the connect
event listener.
我怀疑这是一个时间问题。当你打电话时。连接到Redis的连接已经建立,所以连接事件不会触发。调用redis.createClient()将尝试立即连接,它不会等待您注册连接事件侦听器。
Off the top of my head, something like this should work:
在我的脑海中,像这样的东西应该是有用的:
connectToServer: callback => {
if (client.connected) {
console.log(chalk.red('Already connected to Redis'));
callback();
}
else {
client.on('connect', () => {
console.log(chalk.red('Connected to Redis'));
callback();
}
}
)
I'm not sure whether this will work if the client
is in the process of reconnecting. It's also quite misleading having a function called connectToServer
when it isn't responsible for kicking off the connection. You might want to consider tweaking your code so that createClient
is called inside connectToServer
, more like how you have it with Mongo.
如果客户正在重新连接,我不确定这是否有效。当一个名为connectToServer的函数不负责启动连接时,它也具有相当的误导性。您可能想要考虑调整代码,以便在connectToServer中调用createClient,更类似于如何使用Mongo。
#1
2
I suspect this is a timing issue. By the time you call redisUtil.connectToServer
the connection to Redis will already have been established, so the connect
event won't fire. Calling redis.createClient()
will attempt to connect straight away, it won't wait for you to register the connect
event listener.
我怀疑这是一个时间问题。当你打电话时。连接到Redis的连接已经建立,所以连接事件不会触发。调用redis.createClient()将尝试立即连接,它不会等待您注册连接事件侦听器。
Off the top of my head, something like this should work:
在我的脑海中,像这样的东西应该是有用的:
connectToServer: callback => {
if (client.connected) {
console.log(chalk.red('Already connected to Redis'));
callback();
}
else {
client.on('connect', () => {
console.log(chalk.red('Connected to Redis'));
callback();
}
}
)
I'm not sure whether this will work if the client
is in the process of reconnecting. It's also quite misleading having a function called connectToServer
when it isn't responsible for kicking off the connection. You might want to consider tweaking your code so that createClient
is called inside connectToServer
, more like how you have it with Mongo.
如果客户正在重新连接,我不确定这是否有效。当一个名为connectToServer的函数不负责启动连接时,它也具有相当的误导性。您可能想要考虑调整代码,以便在connectToServer中调用createClient,更类似于如何使用Mongo。