简体   繁体   English

无法连接到 MongoClient.connect 内的 Redis

[英]Can not connect to Redis inside MongoClient.connect

I'm having trouble to connect Redis inside MongoClient.connect.我在 MongoClient.connect 中连接 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 server.js:这不起作用

const mongoUtil     = require( './lib/mongoUtil' )
const redisUtil     = require( './lib/redisUtil' )

mongoUtil.connectToServer( err => {

  redisUtil.connectToServer( () => {
    // some server code
  })

})

server.js: this works server.js:这有效

const mongoUtil     = require( './lib/mongoUtil' )
const redisUtil     = require( './lib/redisUtil' )

redisUtil.connectToServer( () => {
  // some server code
})    

mongoUtil.connectToServer( err => {

})

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.当您调用redisUtil.connectToServer ,与 Redis 的连接已经建立,因此不会触发connect事件。 Calling redis.createClient() will attempt to connect straight away, it won't wait for you to register the connect event listener.调用redis.createClient()将尝试立即连接,它不会等待您注册connect事件侦听器。

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.如果client端正在重新连接,我不确定这是否有效。 It's also quite misleading having a function called connectToServer when it isn't responsible for kicking off the connection.当它不负责启动连接时,有一个名为connectToServer的函数也非常具有误导性。 You might want to consider tweaking your code so that createClient is called inside connectToServer , more like how you have it with Mongo.您可能需要考虑调整您的代码,以便在connectToServer调用createClient ,更像是使用 Mongo 的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM