Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "ERR syntax error" on set using node-redis

Tags:

node.js

redis

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
like image 980
vikramaditya234 Avatar asked Mar 13 '26 02:03

vikramaditya234


1 Answers

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

like image 194
vikramaditya234 Avatar answered Mar 15 '26 05:03

vikramaditya234



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!