0
votes

I need to return the replies from this function and use the replies in other function. I am newbie to NodeJs and trying to figure out a simple solution.

    var getKeys = function(key){
      var addkey = key + '*'
      myClient.keys(addkey, function (err, replies) {
        console.log(replies);
      });
    }

Question 2:

Is there a way to take variable inside the node_redis function?

Example: redis_get -> Defined function for getting values

thingsUUID[i] = thingsUUIDKey[i].split("_").pop()
      redis_get("key", function(redis_items) {
        console.log(thingsUUID[i]);
});

Inside redis_get thingsUUID is undefined. I want to concatenate the thingsUUID and the result redis_items

3

3 Answers

2
votes

you could add the callback that is used in the myClient.keys function to your getKeys function like this:

var getKeys = function(key, callback){
    var addkey = key + '*'
    myClient.keys(addkey, callback);
}

getKeys("EXAMPLE_KEY",  function (err, replies) {

    if (err) console.error(err);

    console.log(replies);
});

As myClient.keys requires a callback it is async and you can't return the response of this back into a value.

This is a good resource to get an overview about how callbacks work: https://github.com/maxogden/art-of-node#callbacks

0
votes

I'm not quite sure what you want to do, but if your variable thinksUUID is defined outside redis_get it should be accessible inside the callback:

var thinksUUID = [1,2,3,4];

var getKeys = function(key, callback){
    var addkey = key + '*'
     myClient.keys(addkey, callback);
}

getKeys("EXAMPLE_KEY",  function (err, replies) {

    if (err) console.error(err);

    replies.forEach(function(item, index){
        console.log(item);
        console.log(thinksUUID[index]);
    });
});

If you want to hand over the variable thinksUUID to your defined function redis_get you need to change your signature of redis_get(key, callback) to redis_get(key, thinksUUID, callback)

0
votes
// using Node.js >= v8 built-in utility
const { promisify } = require('util');
// forcing function to return promise
const getAsync = promisify(redisClient.get).bind(redisClient);
const value = await getAsync(key);
console.log('value of redis key', value)