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?
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.
You could also try with the redis-stack docker container
# Create a docker-compose.yaml placed in root dir with following description
# Then just run >> docker compose up
version: "3.9"
services:
redis:
image: "redis/redis-stack:edge"
ports:
- "6379:6379"
environment:
- "REDIS_ARGS=--appendonly yes"
volumes:
- ./data:/data
deploy:
replicas: 1
restart_policy:
condition: on-failure
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With