2

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

Guy Korland
  • 9,139
  • 14
  • 59
  • 106
Webwoman
  • 10,196
  • 12
  • 43
  • 87
  • if I understand well you have to reiterated for each object, it's very few effective I assume, hope there is more effective way to achieve that – Webwoman Mar 03 '19 at 19:19

1 Answers1

3

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.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
  • Hey Itamar, okay glad if I'm on the path, but my current sample returns me: `ERR missing key at non-terminal path level`, I have tried to googling, but nothing found to handle the problem so far, maybe you have an hint? – Webwoman Mar 03 '19 at 20:48
  • in my test it seems I have to reiterate to create each level one after another manually ,after having create the root, but I assume there is a building function to handle that surely – Webwoman Mar 03 '19 at 21:14