redis.hset疑问
API中给的hset的demo如下:
var redis = require('redis'),
client = redis.createClient(6379, '192.168.0.218');
client.hset("flights", "784-44871820|CZ455", "2014-06-10", redis.print);
我的疑问是,redis.print输出的是执行的结果,如果这个操作的过程中出现错误了该怎么捕获啊(除了try…catch)?没有如hkeys的用法吗?
client.hkeys("hash key", function (err, replies) {
console.log(replies.length + " replies:");
replies.forEach(function (reply, i) {
console.log(" " + i + ": " + reply);
});
client.quit();
});
3 回复
可以这么写的.
client.hset("flights", "784-44871820|CZ455", "2014-06-10", function (err, replies) {
if (err) {
console.log('err', err);
} else {
console.log('replies', replies);
}
client.quit();
});
果然可以了,谢谢哥们儿!
redis里的源码是这样的:
function print(err, reply){ if(err){ // Aerror always begins with Error console.log(err.toString()); }else{ console.log('Reply: ’ + reply); } }
所以 redis.print 也是可以输出错误的