1

我试图在 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 未知命令 JSON.SET,args 开头为:test_node, ., {"node":"blah"},

如何修复?

4

1 回答 1

3

有几个潜在的原因:

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

redis-cli info modules

输出应该包含module:name=ReJSON,ver=...并且您应该能够在 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 模块是旧版本。

npm -v redis
8.1.4

npm upgrade redis如果你的年纪大了试试。

错误看起来像一个,来自 Redis 服务器,所以问题 #1 是最可能的原因。

于 2022-01-05T14:19:08.230 回答