1

I am learning node.js (and express framework) & here is a basic newbie question about redis & node.js. How to pass redis data to templates? What should I correct in my script, so I could display the value of teststring in a template?

app.get('/', function(req, res){
  res.render('index', {
    test: redisclient.get("teststring"),
  });
});

Thanks in advance!

1 Answer 1

5

Since node.js modules (including the one for redis) tends to be non-blocking and asynchronous, they are returning results in callbacks. Try it this way (I also recommend to read this article regarding asynchronous code and callbacks):

app.get('/', function(req, res) {
  redisclient.get("teststring", function(error, response) {
    if(response) {
      res.render('index', {
        test: response,
      });
    } else {
      res.render('index', {
        test: error,
      });
    }
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Why wouldn't you check for error? Could there not be a response and an error?
@Chance - if there is an error, response should be undefined, therefore else clausule would be executed.

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.