2017-06-29 77 views
10

węzeł v: 8.1.2użytku Węzeł-Redis z węzła 8 util.promisify

używam Redis klienta node_redis z węzłem 8 util.promisify, bez blurbird.

redis.get zwrotna jest ok, ale promisify komunikat o błędzie typu get

TypeError: Cannot read property 'internal_send_command' of undefined
at get (D:\Github\redis-test\node_modules\redis\lib\commands.js:62:24)
at get (internal/util.js:229:26)
at D:\Github\redis-test\app.js:23:27
at Object. (D:\Github\redis-test\app.js:31:3)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)

mój kod testowy

const util = require('util'); 

var redis = require("redis"), 
    client = redis.createClient({ 
     host: "192.168.99.100", 
     port: 32768, 
    }); 

let get = util.promisify(client.get); 

(async function() { 
    client.set(["aaa", JSON.stringify({ 
     A: 'a', 
     B: 'b', 
     C: "C" 
    })]); 

    client.get("aaa", (err, value) => { 
     console.log(`use callback: ${value}`); 
    }); 

    try { 
     let value = await get("aaa"); 
     console.log(`use promisify: ${value}`); 
    } catch (e) { 
     console.log(`promisify error:`); 
     console.log(e); 
    } 

    client.quit(); 
})() 

Odpowiedz

15

zmieniając let get = util.promisify(client.get);

do let get = util.promisify(client.get).bind(client);

rozwiązać go za ja :)

+0

dziękuję! został rozwiązany, to jest problem "ten" [this.internal_send_command] (https://github.com/NodeRedis/node_redis/blob/ff9b727609ea125919828f7373e40082fd432eec/lib/commands.js#L62) bez powiązania "this" jest niezdefiniowany –