Node.js request method callback -
this question has answer here:
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
Post a Comment