Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Using redis with node.js (express)

Using redis with node.js (express)

Problem

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!

Problem courtesy of: skazhy

Solution

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,
      });
    }
  });
});
Solution courtesy of: yojimbo87

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Using redis with node.js (express)

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×