I'm using redis using node (node_redis module) and am trying to wrap my retrieval code for debugging and DRY, and having trouble understanding why the following function isnt' working.
I am a bit new to more than basic async with javascript, so I don't think this has anything to do with anything other than bad js.
This works fine but having a wrapper is pretty pointless:
// wrapper
function asyncLoad(className, id, callback) {
redisClient.hget(className, id, callback);
}
// execution
asyncLoad('Person',1234,function(err,res) {
if (err) {
console.log(err);
} else {
var obj = JSON.parse(res);
console.log(obj);
}
});
I thought it would be useful for debugging and repetition if I could do this, but I'm definitely doing something wrong...
// wrapper
function asyncLoad2(className, id, callback) {
redisClient.hget(className, id, function(err,res,callback) {
console.log(callback);
if (err) {
console.log(err);
} else {
var obj = JSON.parse(res);
callback(obj);
}
});
}
// execution
asyncLoad2('Person',1234,function(obj) {
//simpler way to get back a parsed object with error handling already done
}
Thanks in advance! PS - I'm aware that this is really lame error handling; I'm not expecting to use it all the time, just for select things and especially during debugging.
varbeforeobj, in the second snippet you've misspelledParse(should be lowercased)