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
client.get = util.promisify(client.get).bind(client)const data = await client.get("ke"); console.log(data);undefined