vul*_*ld0 11 redis node.js redisjson
我试图将 JSON 设置为 Redis 中的值,但收到以下代码的错误:
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();
Run Code Online (Sandbox Code Playgroud)
错误:
ReplyError: ERR 未知命令 JSON.SET,参数开头为:test_node, ., {"node":"blah"},
如何解决?
Ant*_*ton 16
有几个潜在的原因:
#1 RedisJSON 模块未安装。尝试:
redis-cli info modules
Run Code Online (Sandbox Code Playgroud)
输出应包含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\"}"
Run Code Online (Sandbox Code Playgroud)
#2 redis npm 模块是旧版本。
npm -v redis
8.1.4
Run Code Online (Sandbox Code Playgroud)
npm upgrade redis如果你的比较旧,请尝试一下。
该错误看起来像是来自 Redis 服务器的错误,因此问题 1 是最有可能的原因。