为什么我在Redis中得到“ReplyError:ERR unknown command JSON.SET”?

myss37ts  于 2023-11-16  发布在  Redis
关注(0)|答案(2)|浏览(197)

我试图在Redis中将JSON设置为值,并得到以下代码的错误:

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();

字符串
错误代码:
ReplyError:ERR unknown command JSON.SET,with args beginning with:test_node,.,{“node”:“blah”},
怎么能修好呢?

svmlkihl

svmlkihl1#

有几个潜在的原因:

1未安装RedisJSON模块。请尝试:

redis-cli info modules

字符串
输出应该包含module:name=ReJSON,ver=...,你应该能够在redis-configuration中执行以下操作:

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模块是旧版本。

npm -v redis
8.1.4


npm upgrade redis如果你的比较老
这个错误看起来像是来自Redis服务器,所以问题#1是最有可能的原因。

jum4pzuy

jum4pzuy2#

您也可以尝试使用redis-stack docker容器

# 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

字符串

相关问题