2017-06-29 47 views
10

節點-v:8.1.2使用節點redis的與節點8 util.promisify

我使用Redis的客戶端與node_redis節點8 util.promisify,沒有blurbird。

回調redis.get是好的,但promisify型得到錯誤信息

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)

我的測試代碼

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(); 
})() 

回答

15

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

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

解決它我:)

+0

謝謝!它解決了,這是'this''問題 [this.internal_send_command](https://github.com/NodeRedis/node_redis/blob/ff9b727609ea125919828f7373e40082fd432eec/lib/commands.js#L62)沒有綁定'this'是undefined –