2016-03-21 11 views
0

redis sentinelクライアントプログラム - 最初のプログラムでは、赤いセンチネルオブジェクトを作成したままにして、KEYを設定するとうまく動作します。しかし、2番目のプログラムclient.on( 'connect'、runSample(client))を観察すると、私はクライアントオブジェクト(redis sentinel client)を渡しています、その接続パラメータrunSample。このために私は次のエラーを取得しています。..JSオブジェクトをredis - sentinal on connectイベントで送信する方法

エラー詳細https://github.com/DocuSignDev/node-redis-sentinel-client/blob/master/index.js

RedisSentinelClient.prototype.send_command undefined 
    /node_modules/redis-sentinel-client/index.js:293 
    return client.send_command.apply(client, arguments); 
     ^
    TypeError: Cannot read property 'send_command' of undefined 
at RedisSentinelClient.send_command (/node_modules/redis-sentinel-client/index.js:293:16) 
at RedisSentinelClient.(anonymous function).RedisSentinelClient.(anonymous function) (/node_modules/redis-sentinel-client/index.js:307:23) 
at runSample (/msg/lb4.expire.onefile.2m.notworking.js:25:13) 
at init (/msg/lb4.expire.onefile.2m.notworking.js:16:26) 
at Object.<anonymous> (/msg/lb4.expire.onefile.2m.notworking.js:75:1) 
at Module._compile (module.js:460:26) 
at Object.Module._extensions..js (module.js:478:10) 
at Module.load (module.js:355:32) 
at Function.Module._load (module.js:310:12) 
at Function.Module.runMain (module.js:501:10) 
RajRajen:pubsub.local rajrajen$ 

まず作業プログラム

..

'use strict'; 
var client = getRedisSentinelObject(); 
client.on('error', function(err) { 
    console.log('Error ' + err); 
}); 

client.on('connect', runSample); 

function runSample() { 

    var allStrings = '{abc:123}'; 


    client.get(allStrings, function(err, reply) { 
     if (reply) { 
     console.log('Key is ' + reply.toString()); 
     client.ttl(allStrings, writeTTL); 
    } else { 
     console.log('string key expired or not set before!'); 
     // Set a value 
     client.set(allStrings, allStrings); 
     // Expire in 3 seconds 
     client.expire(allStrings, 3); 
    } 
    client.quit(); 
    }); 
} 

function getRedisSentinelObject() { 

    var redisSentinelHost = process.env.REDIS_SENTINEL_SERVICE_HOST; 
    var redisSentinelPort = process.env.REDIS_SENTINEL_SERVICE_PORT; 

    var options = { 
     'master_debug': false 
    }; 


    var redisSentinelMasterDebug = process.env.REDIS_SENTINEL_MASTER_DEBUG; 


if (typeof redisSentinelMasterDebug !== "undefined") { 
    if (redisSentinelMasterDebug === "true") { 
     options.master_debug = true; 
    } 
} 

console.log('redisSentinelHost ', redisSentinelHost, 'redisSentinelPort ', redisSentinelPort); 

var RedisSentinel = require('redis-sentinel-client'); 
var sentinelClient = RedisSentinel.createClient(redisSentinelPort, redisSentinelHost, options); 
console.log('sentinelClient ', sentinelClient); 

return sentinelClient; 
} 

function writeTTL(err, data) { 
    console.log('I live for this long yet: ', data); 
} 

第二プログラムは、つまり、動作していません

'use strict'; 

    function init() { 

var client = getRedisSentinelObject(); 


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

client.on('connect', runSample(client)); 

} 

function runSample(client1) { 


var allStrings = '{abc:123}'; 


client1.get(allStrings, function(err, reply) { 
    if (reply) { 
     console.log('Key is ' , reply.toString()); 
     client1.ttl(allStrings, writeTTL); 
    } else { 
     console.log('string key expired or not set before!'); 
     // Set a value 
     client1.set(allStrings, allStrings); 
     // Expire in 3 seconds 
     client1.expire(allStrings, 2); 
    } 
    // client1.quit(); 
}); 


} 

function getRedisSentinelObject() { 

var redisSentinelHost = process.env.REDIS_SENTINEL_SERVICE_HOST; 
var redisSentinelPort = process.env.REDIS_SENTINEL_SERVICE_PORT; 

var options = { 
    'master_debug': false 
}; 


var redisSentinelMasterDebug = process.env.REDIS_SENTINEL_MASTER_DEBUG; 

if (typeof redisSentinelMasterDebug !== "undefined") { 
    if (redisSentinelMasterDebug === "true") { 
     options.master_debug = true; 
    } 
} 

console.log('redisSentinelHost ', redisSentinelHost, 'redisSentinelPort ', redisSentinelPort); 

var RedisSentinel = require('redis-sentinel-client'); 
var sentinelClient = RedisSentinel.createClient(redisSentinelPort, redisSentinelHost, options); 
console.log('sentinelClient ', sentinelClient); 

return sentinelClient; 
} 

function writeTTL(err, data) { 
console.log('I live for this long yet: ', data); 
} 

init(); 

おかげ

答えて

1

問題

あなたが持っている行例1で

client.on('connect', runSample); 

この行は、クライアントが接続したときに実行する機能を付加します。すべてが良いです。この関数は、クライアントが接続するまで実行されないことに注意してください。

は、実施例2では、​​同じ行は次のようになります

client.on('connect', runSample(client)); 

このラインでは、runSample方法が直ちにが実行されます。この関数呼び出し(この場合はundefined)の結果は、client.onに渡されます。私たちはここに手による評価の少しを行うのであれば、Javascriptをに沸く:

client.on('connect', undefined); 

あなたは、Redisのを言っているので、これは失敗した「あなたが接続すると、何もありません」。この問題を解決するために

簡単に修正

最も簡単な方法は、クロージャ(それはスコープの吸収機能)を使用することです。これは、あなたがすることを意味する可能性が最も高いです。

client.on('connect', function() { 
    runSample(client) 
}); 

より複雑な修正

あなたは物事はもう少し複雑にしたい場合は、このような関数を作ることができる:

function buildRunner (client) { 
    return function runTests() { 
     var allStrings = '{abc:123}'; 


     client.get(allStrings, function(err, reply) { 
      if (reply) { 
       console.log('Key is ' + reply.toString()); 
       client.ttl(allStrings, writeTTL); 
      } else { 
       console.log('string key expired or not set before!'); 
       // Set a value 
       client.set(allStrings, allStrings); 
       // Expire in 3 seconds 
       client.expire(allStrings, 3); 
      } 
      client.quit(); 
     }); 
    } 
} 

など、それを使用します。

client.on('connect', buildRunner(client)); 

buildRunnerは即時に実行されます前と同じように違いはbuildRunnerがに渡される関数を返すことです。もう一度手作業で評価すると、

client.on('connect', function runTests() { ... }) 
        //^runTests is holding on to an internal copy of `client` because 
        // `client` existed back when we built runTests 
        // up in `buildRunner` 
+0

ありがとう@JoshWillik。それをチェックします。 –

+0

ありがとうございましたJoshWillik ..それは働いた。あなたの助けに感謝します。 –

関連する問題