3

I am unable to set and get key in redis using node-redis. But set and get works well when using redis-cli. This is my code:

const redis = require('redis');
const util = require('util');

const GLOBAL_KEY = 'lambda-test';
const redisOptions = {
    host: process.env.REDIS_HOST,
    port: process.env.REDIS_PORT
}

try{
    test();
}
catch(err) {
    console.log('catch err ' + err);
}

async function test() {
    var client = redis.createClient(redisOptions);
    client.on("error", function(error) {
        console.error(error);
    });
    client.get = util.promisify(client.get);
    client.set = util.promisify(client.set);
    await client.set("ke", "test1", redis.print).catch(function(err){console.log('err: '+ err);});
    await client.get("ke", redis.print).catch(function(err){console.log('err: '+ err);});
}

Output:

err: ReplyError: ERR syntax error
err: ReplyError: ERR wrong number of arguments for 'get' command

Can't figure what the reason is, can anyone help me figure what I am missing?

Update: I tried using bind, but no luck:

    client.get = util.promisify(client.get).bind(client);
    client.set = util.promisify(client.set).bind(client);

Also I have enabled the DEBUG to get extra logs:

2021-04-20T07:48:36.279Z Queueing set for next server connection.
2021-04-20T07:48:36.303Z Stream connected XXXXX:6379 id 0
2021-04-20T07:48:36.303Z Checking server ready state...
2021-04-20T07:48:36.304Z Send XXXXXX:6379 id 0: *1
$4
info

2021-04-20T07:48:36.305Z Net read XXXXX:6379 id 0
2021-04-20T07:48:36.306Z Redis server ready.
2021-04-20T07:48:36.306Z on_ready called XXXXX:6379 id 0
2021-04-20T07:48:36.307Z Sending offline command: set
2021-04-20T07:48:36.307Z Send XXXXX:6379 id 0: *4
$3
set
$2
ke
$5
test1
$187
function print (err, reply) {
    if (err) {
        // A error always begins with Error:
        console.log(err.toString());
    } else {
        console.log('Reply: ' + reply);
    }
}

2021-04-20T07:48:36.307Z Net read XXXXX:6379 id 0
err: ReplyError: ERR syntax error
2021-04-20T07:48:36.309Z Send XXXXX:6379 id 0: *3
$3
get
$2
ke
$187
function print (err, reply) {
    if (err) {
        // A error always begins with Error:
        console.log(err.toString());
    } else {
        console.log('Reply: ' + reply);
    }
}

2021-04-20T07:48:36.310Z Net read XXXXX:6379 id 0
err: ReplyError: ERR wrong number of arguments for 'get' command
8
  • Try client.get = util.promisify(client.get).bind(client) Commented Apr 20, 2021 at 7:06
  • Thanks that didn't help, please check the update Commented Apr 20, 2021 at 7:53
  • Use const data = await client.get("ke"); console.log(data); Commented Apr 20, 2021 at 7:56
  • it returns undefined Commented Apr 20, 2021 at 8:47
  • I think if you want to properly bind client.get you should do util.promisify(client.get.bind(client)) - ie this is the reference to the client.get function that should be bound, not the function returned by the promisify method. Not sure that would solve your issue though. Commented Apr 20, 2021 at 8:53

1 Answer 1

2

Thanks all for suggestion. I posted the question as issue on the repo https://github.com/NodeRedis/node-redis/issues/1599 and got the answer from leibale:

async function test() {
    const client = redis.createClient(redisOptions);
    client.on("error", function(error) {
        console.error(error);
    });
    client.get = util.promisify(client.get).bind(client);
    client.set = util.promisify(client.set).bind(client);

    try {
        console.log('SET reply:', await client.set("ke", "test1"));
    } catch (err) {
        console.error(err);
    }

    try {
        console.log('GET reply:', await client.get("ke"));
    } catch (err) {
        console.error(err);
    } 
}

The issue was bind and use of redis.print as a callback

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.