Database 从gun获取流式数据

Database 从gun获取流式数据,database,gun,Database,Gun,on应该是从路径或键流式传输数据。但当我将数据放在路径上时,我看不到更新的流 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/gu

on应该是从路径或键流式传输数据。但当我将数据放在路径上时,我看不到更新的流

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'});
但是,它仅用于一次性目的,不用于流式传输。注意,您可以向.valfunctiondata{}传递一个回调,该回调将覆盖默认的便利记录器。

更新:因为使用val的Gun v0.391也需要回调。不再提供自动日志记录