Node.js request method callback -


client.get method not work in redis node.js

//blog function module.exports = {   index: function () {      var data = new object();     data.ip = base.ip();      var redis = require("redis");     var client = redis.createclient(6379,'192.168.33.10');      client.get("foo",function(err,reply,callback)     {       var x=reply.tostring();     });      data.y=x;       return data;   }   }; 

data.y="bar" expects

x not defined.

where problem?

your problem client.get asynchronous method, can't return from. need sort of asynchronous control flow, such callbacks.

//blog function module.exports = {   index: function (callback) {     var data = new object();     data.ip = base.ip();     var redis = require("redis");     var client = redis.createclient(6379, '192.168.33.10');     client.get("foo",function(err,reply) {       if(err) return callback(err);       data.y = reply.tostring();       callback(null, data);     });   } }; 

you'd use like

var obj = require('./whatever'); obj.index(function(err, result) {   //result contain data. }); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -