2017-06-29 44 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 –