I tried to use the "@redis/json" library to set json object in Redis.
const redisClient = await createClient({
url: `redis://${redis.host}:${redis.port}`,
});
await redisClient.connect();
redisClient.json.set() //getting json undefined error
But redisClient doesn’t recognize the json object.
The docs are not clear
2
Answers
@Simon answer is correct when installing the entire 'redis' library. This answer works when importing only the @redis/client + @redis/json
if you are using typescript the redisClient type is RedisClientType<{ json: typeof RedisJsonModule }, RedisFunctions, RedisScripts>
The documentation definitely needs work, and that’s something we’re putting time into. There are several example scripts here that we try to keep up to date with all the new functionality:
https://github.com/redis/node-redis/tree/master/examples
Here’s an example of how to use the JSON commands:
https://github.com/redis/node-redis/blob/master/examples/managing-json.js
Your code looks OK to me, you shouldn’t need to
await
thecreateClient
call though.Here’s a basic working example:
output:
package.json:
index.js: