10

I was trying to set JSON as value in Redis, and I am getting an error for the following code:

const createClient = require('redis');


async function redisJSONDemo () {
  try {
    const TEST_KEY = 'test_node';

    const client = createClient.createClient();
    await client.connect();

    // RedisJSON uses JSON Path syntax. '.' is the root.
      await client.json.set(TEST_KEY, '.', { node: 'blah' });
    const value = await client.json.get(TEST_KEY, {
      // JSON Path: .node = the element called 'node' at root level.
      path: '.node'
    });

    console.log(`value of node: ${value}`);

    await client.quit();
  } catch (e) {
    console.error(e);
  }
}

redisJSONDemo();

Error:

ReplyError: ERR unknown command JSON.SET, with args beginning with: test_node, ., {"node":"blah"},

How can it be fixed?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
vuld0
  • 184
  • 1
  • 1
  • 9
  • Possibly relevant: [ERR unknown command 'JSON.SET' - Rejson/Redis](https://stackoverflow.com/q/52094110) | https://github.com/RedisJSON/RedisJSON/issues/214 | https://github.com/RedisJSON/RedisJSON/issues/45 – VLAZ Jan 05 '22 at 12:21

1 Answers1

13

There few potential causes:

#1 RedisJSON module is not installed. Try:

redis-cli info modules

Output should contain module:name=ReJSON,ver=... and you should be able to do the following in the redis-cli:

127.0.0.1:6379> json.set test_node $ '{ "node": "blah" }'
OK
127.0.0.1:6379> json.get test_node
"{\"node\":\"blah\"}"

#2 redis npm module is of an older version.

npm -v redis
8.1.4

try npm upgrade redis if yours is older.

The error looks like one, coming from the Redis server, so problem #1 is the most likely cause.

Anton
  • 3,587
  • 2
  • 12
  • 27
  • I did run the command redis-cli info modules. and I do not get any output. And yes I am not able to "json set" in the redis-cli. Can you help me further or could provide any resources? Thanks any way. – vuld0 Jan 05 '22 at 16:03
  • 1
    I'm glad that helped! RedisJSON is a module that needs to be installed/activated on Redis. Take a look at https://oss.redis.com/redisjson/ - the easiest way to try it would be either Redis Cloud or the docker image. – Anton Jan 05 '22 at 16:16
  • 1
    it should be -p 6369:6379 in the docker command. 6379 (second number) is the standard port inside the docker container and 6369 is how you will see it on the host machine. – Anton Jan 05 '22 at 18:17
  • hey it works. I checked it with `redis-commander` too. Thanks for the help @Anton. – vuld0 Jan 05 '22 at 18:26