我正在使用nowjs和node_redis.我想创造一些非常简单的东西.但到目前为止,该教程让我空白,因为他们只执行console.log().
//REDIS var redis = require("redis"), client = redis.createClient(); client.on("error", function (err) { console.log("Error "+ err); }); client.set("card", "apple"); everyone.now.signalShowRedisCard = function() { nowjs.getGroup(this.now.room).now.receiveShowRedisCard(client.get("card").toString()); }
在我的客户方:
now.receiveShowRedisCard = function(card_id) { alert("redis card: "+card_id); }
警报只给出"真实" - 我期望获得"苹果"关键"卡"的价值.
有任何想法吗?
您正尝试以同步方式使用异步库.这是正确的方法:
//REDIS var redis = require("redis"), client = redis.createClient(); client.on("error", function (err) { console.log("Error "+ err); }); client.set("card", "apple", function(err) { if (err) throw err; }); everyone.now.signalShowRedisCard = function() { var self = this; client.get("card", function (err, res) { nowjs.getGroup(self.now.room).now.receiveShowRedisCard(res); }); }