作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
on()
应该从路径或键流式传输数据 1 .但是当我将
数据放在我的路径上时,我没有看到更新的流。
var myData = Gun('https://gunjs.herokuapp.com/gun')
.get('example/demo/set');
myData.on();
myData.put({hello:'world'});
最佳答案
.on()
是一个异步函数,因此您需要将代码更新为如下所示:
var myData = Gun('https://gunjs.herokuapp.com/gun')
.get('example/demo/set');
myData.on(function(data){
console.log("update:", data);
});
myData.put({hello:'world'});
希望对您有所帮助!
如果您是编程新手,上述代码中的“匿名函数”(通常称为回调)可能有些令人困惑。上面的代码也可以重写为这样,它具有完全相同的行为:
var myData = Gun('https://gunjs.herokuapp.com/gun')
.get('example/demo/set');
var cb = function(data){
console.log("update:", data);
};
myData.on(cb);
myData.put({hello:'world'});
出于调试目的,还有一个.val()
便利函数,它会自动为您记录数据:
var myData = Gun('https://gunjs.herokuapp.com/gun')
.get('example/demo/set');
myData.on().val()
myData.put({hello:'world'});
然而,它是用于一次性目的,而不是用于流式传输。请注意,您可以传递 .val(function(data){})
一个回调,它将覆盖默认的便利记录器。
关于database - 从枪中获取流数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875457/
我是一名优秀的程序员,十分优秀!