【问题标题】:use node-redis with node 8 util.promisify将 node-redis 与 node 8 util.promisify 一起使用
【发布时间】:2017-12-02 14:08:48
【问题描述】:

节点-v:8.1.2

我使用 redis 客户端 node_redis 和节点 8 util.promisify ,没有 blurbird。

回调redis.get没问题,但是promisify类型得到错误信息

TypeError:无法读取未定义的属性“internal_send_command”
在获取 (D:\Github\redis-test\node_modules\redis\lib\commands.js:62:24)
在获取 (internal/util.js:229:26)
在 D:\Github\redis-test\app.js:23:27
在对象。 (D:\Github\redis-test\app.js:31:3)
在 Module._compile (module.js:569:30)
在 Object.Module._extensions..js (module.js:580:10)
在 Module.load (module.js:503:32)
在 tryModuleLoad (module.js:466:12)
在 Function.Module._load (module.js:458:3)
在 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();
})()

【问题讨论】:

    标签: node.js redis promise


    【解决方案1】:

    您还可以使用 Blue Bird 库加上猴子补丁来解决问题。 例如:

    const bluebird = require('bluebird')
    const redis = require('redis')
    async connectToRedis() {
         // use your url to connect to redis
         const url = '//localhost:6379'
         const client = await redis.createClient({
                    url: this.url
                }) 
         client.get = bluebird.promisify(client.get).bind(client);
         return client
       }
    // To connect to redis server and getting the key from redis
    connectToRedis().then(client => client.get(/* Your Key */)).then(console.log)
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 node v8 或更高版本,您可以使用 util.promisify 如:

      const {promisify} = require('util');
      const getAsync = promisify(client.get).bind(client); // now getAsync is a promisified version of client.get:
      
      // We expect a value 'foo': 'bar' to be present
      // So instead of writing client.get('foo', cb); you have to write:
      return getAsync('foo').then(function(res) {
          console.log(res); // => 'bar'
      });
      

      或使用异步等待:

      async myFunc() {
          const res = await getAsync('foo');
          console.log(res);
      }
      

      无耻地从redis official repo中剔除

      【讨论】:

        【解决方案3】:

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

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

        帮我解决了:)

        【讨论】:

        • 谢谢!解决了,就是this问题this.internal_send_command没有绑定this是未定义的
        • 只需将client.get 替换为您喜欢的命令即可;)
        猜你喜欢
        • 2020-05-04
        • 2014-02-13
        • 2011-06-02
        • 2013-04-24
        • 2015-11-08
        • 1970-01-01
        • 1970-01-01
        • 2022-08-13
        • 2020-10-26
        相关资源
        最近更新 更多