Diagathe Josué
Diagathe Josué

Reputation: 12076

Redis ReJSON returns me two errors systematically: missing key at non-terminal path and new object must be created at root

The second error is relatively straighforward to understand. The first is a bit more challenging.

I have tried different combination to overcome this error but none improvement occurs. My console returns me:

// console log > first promise { ReplyError: ERR new objects must be created at the root at parseError (node_modules/redis-parser/lib/parser.js:193:12) at parseType (node_modules/redis-parser/lib/parser.js:303:14) command: 'JSON.SET', args: [ 'jsonTest7', '.user.here.now', '{".nestedValue": "I am a nested value"}' ], code: 'ERR' } // console log > second promise // console log > jsonTest7 response: null

Here my snippet.js:

const redis=require("redis"); 
rejson = require('redis-rejson');
const {promisify} = require('util'); 

rejson(redis); /* important - this must come BEFORE creating the client */
let client= redis.createClient({
    port:6380,
    host:'localhost', 
});  

const setAsync = promisify(client.json_set).bind(client);
const getAsync = promisify(client.json_get).bind(client);
const existsAsync= promisify(client.exists).bind(client);
client.exists('jsonTest2', function(err, reply) {
    if (reply === 1) {
        return true
    } else {
        return false
    }
});

async function isExist(object){ 
    var isExist= await existsAsync(object).then(data=> data)
        .catch((err) => console.error(err)); 
    console.log("isExist: ", typeof isExist)
    if(isExist !== 1) { 
        console.log("creating object...")
        await setAsync(object, '.', '{"here":"something"}');
        console.log("object created: " + object)
    } 
}
async function myFunc(object, rootKey) { 
    console.log("then 1")
    await isExist(object)
    await setAsync(object,  ".user.here.now", '{".nestedValue": "I am a nested value"}')
                .catch((err) => console.error(err));
    console.log("then 2")
    const res = await getAsync(object,  '.user.here.now')
                .catch((err) => console.error(err)); 
    console.log(object + " response: ", res) 

}
myFunc("jsonTest7")

Any hint would be great, thanks

Upvotes: 2

Views: 2649

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50082

The first error means that you're trying to create a new document - i.e. the RedisJSON key does not exist - but are only supplying a subdocument (i.e. '.user.here.now'). New keys must be set to the root ('.') level like your current code sample shows.

Upvotes: 3

Related Questions