从redis / index.js文件中捕获连接错误throw(n)

时间:2013-11-29 07:08:49

标签: node.js express connection redis database-connection

我通过heroku连接到redis。

var redisClient = require('redis').createClient({
    host: 'http://networkinglawyer.in/home/redis',
    port: 9374,
    db: 0,
    requirepass: 'abcdefghijklmnopqrstuvwxyz'
  });

redis.index.js文件是throw err如何抓住它将其打印到控制台上?

编辑: - 错误是

2013-11-29T07:18:38.255695+00:00 app[web.1]: userSchema defined
2013-11-29T07:18:38.262109+00:00 app[web.1]: questionSchema defined
2013-11-29T07:18:38.264414+00:00 app[web.1]: activitySchema defined
2013-11-29T07:18:38.273814+00:00 app[web.1]: connection error: [Error: failed to connect to [localhost:27017]]
2013-11-29T07:18:38.282107+00:00 app[web.1]: 
2013-11-29T07:18:38.282867+00:00 app[web.1]:                 throw callback_err;
2013-11-29T07:18:38.282528+00:00 app[web.1]: /app/node_modules/redis/index.js:563
2013-11-29T07:18:38.283575+00:00 app[web.1]:                       ^
2013-11-29T07:18:38.287475+00:00 app[web.1]:     at RedisClient.on_info_cmd (/app/node_modules/redis/index.js:371:35)
2013-1

2 个答案:

答案 0 :(得分:1)

你在redis调用的回调函数中有错误:

// from https://github.com/mranney/node_redis/blob/master/index.js
if (command_obj && typeof command_obj.callback === "function") {
    try {
        command_obj.callback(err);
    } catch (callback_err) {
        // if a callback throws an exception, re-throw it on a new stack so the parser can keep going
        process.nextTick(function () {
            throw callback_err;
        });
    }
}

真正的问题是你无法连接到mongodb,这会在redis-client调用的回调函数中引发异常。

答案 1 :(得分:0)

捕获错误并将其打印到控制台:

redisClient.on('error', function (err) {
    console.log('Error ' + err);
});

您可能还希望稍后添加处理不同形式的错误。