0

My node.js code to connect redis server is like this

    var http = require("http"),
  querystring = require("querystring"),
  redis = require("redis"),
  db = redis.createClient(6379, "127.0.0.1"); 

db.set("Jaime", "Developer", function(){});

http.createServer(function(req, res) {
  var qs = querystring.parse(req.url.split("?")[1]),
    firstName = qs.firstName;

  db.get(firstName, function(err, lastName) {
    var userName = firstName + " " + lastName,
      html = "<!doctype html>" +
        "<html><head><title>Hello " + userName + "</title></head>" +
        "<body><h1>Hello, " + userName + "!</h1></body><h2>I am in this page</h2></html>";

    res.end(html);

  });
}).listen(8000);

my redis server is also running and give a message like this

[3648] 13 Oct 11:43:00 * The server is now ready to accept connections on port 6 379 [3648] 13 Oct 11:43:01 - DB 0: 3 keys (0 volatile) in 4 slots HT. [3648] 13 Oct 11:43:01 - 0 clients connected (0 slaves), 672976 bytes in use [3648] 13 Oct 11:43:06 - DB 0: 3 keys (0 volatile) in 4 slots HT. [3648] 13 Oct 11:43:06 - 0 clients connected (0 slaves), 672976 bytes in use

when I connect it with browser (http://localhost:8000) the server status gives

[3648] 13 Oct 11:45:07 - Accepted 127.0.0.1:1715 [3648] 13 Oct 11:45:10 - DB 0: 3 keys (0 volatile) in 4 slots HT. [3648] 13 Oct 11:45:11 - 1 clients connected (0 slaves), 680892 bytes in use

and the browser out is this

Hello, undefined null! I am in this page

I am a entirely new to this .. I don't know how to make this work please help me.

1
  • 1
    Add a console.log(err); inside the callback. What does that return? Commented Oct 13, 2012 at 6:44

1 Answer 1

1

You have two problems.

First, db.set is an asynchronous operation, but you have an anonymous function in the callback with nothing in it, so there is no guarantee that the operation has completed by the time you get down to the get.

You need to nest the later code inside the db.set callback (can get messy) or use a flow control library for node like async.

Second, you retrieve firstName from query params, and then don't pass anything in ... Hence the first undefined, e.g.http://localhost:8000/?firstName=Jaime (this is actually what is causing the error).

You need to get both fixed for this to work. I'd read up on async operations in node, and async libraries.

(edited to add links).

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

3 Comments

when I give localhost:8000/?firstName=Jaime its giving Hello, Jaime Developer! I am in this page so I think code is working ?? I will go through the link ... thank you for the response. @cliftonc
Your first remark (regarding the fact db.set is asynchronous) is irrelevant here, because commands sent on the same Redis connection will be processed in order (and Redis is single threaded). So the SET is always sent before the GET, and therefore the reply of the GET cannot be processed before the SET. There is no need for callback nesting here ...
You're 100% correct, so I've up voted your comment as many may not realise this ... but if there was any error in the SET (e.g. Redis server memory at limit for example) you would miss it and the GET would then fail.

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.