2017-06-29 6 views
10

ノード-v:8.1.2使用ノード-Redisの8 util.promisify

は私がノード8 util.promisify、無blurbirdでRedisのクライアントnode_redisを使用しています。

コールバックredis.getはOKですが、

がためにそれを解決しpromisify型の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)

私のテストコード

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).bind(client);let get = util.promisify(client.get);

を変更します私:

+0

ありがとう!それは解決されました、それは 'これは問題です [this.internal_send_command](https://github.com/NodeRedis/node_redis/blob/ff9b727609ea125919828f7373e40082fd432eec/lib/commands.js#L62)バインドなしで 'this'は未定義です –

関連する問題