将 node-redis 与 node 8 util.promisify 一起使用 [英] use node-redis with node 8 util.promisify
问题描述
节点 -v : 8.1.2
node -v : 8.1.2
我将 redis 客户端 node_redis 与节点 8 util.promisify 一起使用,没有 blurbird.
I use redis client node_redis with node 8 util.promisify , no blurbird.
回调 redis.get 没问题,但是 promisify 类型获取错误信息
the callback redis.get is ok, but promisify type get error message
类型错误:无法读取未定义的属性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)
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();
})()
推荐答案
更改 let get = util.promisify(client.get);
to let get = util.promisify(client.get).bind(client);
帮我解决了:)
这篇关于将 node-redis 与 node 8 util.promisify 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!