Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use node-redis with node 8 util.promisify

node -v : 8.1.2

I use redis client node_redis with node 8 util.promisify , no blurbird.

the callback redis.get is ok, but promisify type get error message

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)

my test code

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(); })() 
like image 229
wi Yu Avatar asked Jun 29 '17 02:06

wi Yu


People also ask

What is the purpose of the util Promisify () function?

The util. promisify() method basically takes a function as an input that follows the common Node. js callback style, i.e., with a (err, value) and returns a version of the same that returns a promise instead of a callback.

How do I use Redis in node JS?

Create new session. js file in the root directory with the following content: const express = require('express'); const session = require('express-session'); const redis = require('redis'); const client = redis. createClient(); const redisStore = require('connect-redis')(session); const app = express(); app.

What is NodeJS Redis?

Redis, an in-memory database that stores data in the server memory, is a popular tool to cache data. You can connect to Redis in Node. js using the node-redis module, which gives you methods to retrieve and store data in Redis.


1 Answers

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

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

solved it for me :)

like image 57
Jannic Beck Avatar answered Sep 27 '22 20:09

Jannic Beck