0

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 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @driedel. Is it possible for you to give solution for Question 2?. I edited and added the question
0

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)

Comments

0
// 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.