4

my node.js code like this:

var data = "";
//redis client
client.get(key,function(err,value){
    data += value;
});
//output
console.log(data);

BUT, it prints nothing. Why so? and how can i get the data out of the callback function?

thanks a lot.

jackie
  • 85
  • 2
  • 8
  • Have a look at the first part of my answer here: http://stackoverflow.com/a/14220323/218196. It tries to explain the the differences between synchronous and asynchronous code. – Felix Kling Mar 31 '13 at 09:22
  • this post also might help:: http://stackoverflow.com/questions/14110798/cannot-get-valid-list-while-using-redis-with-node-js – Sudhir Bastakoti Mar 31 '13 at 09:23

4 Answers4

4

ANSWER FROM THE DOCUMENTATION for Redis DATA outside the CALLBACK

        const redis = require('redis');
        const redisClient = redis.createClient(process.env.REDIS_URI);

        // 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 outside the callback is', value);
       
Firoj Siddiki
  • 1,649
  • 1
  • 20
  • 22
2

Your passing the redis client a callback which will be called later (when the data is returned over the network), but then right after making the redis call you're trying to print the value before redis has sent you a value. You need to wait for redis to return a value.

Try this

var data = "";
//redis client
client.get(key,function(err,value){
    data += value;
    //output 
    console.log(data);
});
Daniel
  • 38,041
  • 11
  • 92
  • 73
  • 1
    What if I need to use `data` outside of the callback function? How can I make `data` work after the `});` at the end? – Aaron Franke Nov 05 '19 at 18:38
0

Pasting my Redis file that converts callbacks into promises, in case it's helpful. It's in typescript and typesafe. You should receive intellisense and autocomplete.

import redis from "redis"
import { promisify } from "util"
import { redisHost as host, redisPort as port } from "../../configKeys"

const client = redis.createClient({
  port,
  host,
})

client.on("connect", () => {
  console.log(`Redis Client connected on port ${port}`)
})

client.on("ready", () => {
  console.log("Redis Client ready to use..")
})

client.on("error", (err) => {
  console.log(err.message)
})

client.on("end", () => {
  logCommon.debug("Redis Client disconnected")
})

process.on("SIGINT", () => {
  client.quit()
})

export const GET_ASYNC = (promisify<string>(client.GET).bind(
  client
) as unknown) as (key: string) => Promise<string | null>

export const SET_ASYNC = (promisify<string, string, string, number>(
  client.SET
).bind(client) as unknown) as (
  key: string,
  value: string,
  mode: string,
  duration: number
) => Promise<"OK" | null>

export const DELETE_ASYNC = (promisify<string>(client.DEL).bind(
  client
) as unknown) as (key: string) => Promise<number | null>

export default client

Import GET_ASYNC or SET_ASYNC into the required file and you can use it with async-await like you would a promise.

Tom Bombadil
  • 3,455
  • 1
  • 13
  • 24
-1

Your redis binding's client.get function is asynchronous.

It'll call callback function after it completes querying redis server. You should print output in callback.

var data = "";
//redis client
client.get(key,function(err,value){
    data += value;
    //output
    console.log(data);
});
smitrp
  • 1,292
  • 1
  • 12
  • 28
  • 2
    What if I need to use `data` outside of the callback function? How can I make `data` work after the `});` at the end? – Aaron Franke Nov 05 '19 at 18:38