1

gun 0.8.7,Node.js-to-Node.js,没有浏览器。

节点已成功创建并添加到tasks集合中

const Gun = require('gun');
const _ = require('lodash');
require( "gun/lib/path" );

const gun = new Gun({peers:['http://localhost:8080/gun', 'http://localhost:8081/gun']});

const watchers = [
  {
    id: '123',
    type: 'skeleton',
    stat: {
      num: 0
    }
  },
  {
    id: '456',
    type: 'snowmann',
    stat: {
      num: 0
    }
  },
  {
    id: '789',
    type: 'moose',
    stat: {
      num: 0
    },
  }
]; 

const tasks = gun.get('tasks'); 

_.forEach(watchers, function (watcher) {
  let task = gun.get(`watcher/${watcher.id}`).put(watcher);
  tasks.set(task);
});

.map评价结果

a2task { _:
   { '#': 'watcher/123',
     '>': { id: 1506959623558, type: 1506959623558, stat: 1506959623558 } },
  id: '123',
  type: 'skeleton',
  stat: { '#': 'j8acunbf70NblJptwXWa' } }
task { _:
   { '#': 'watcher/456',
     '>':
      { id: 1506959623579.002,
        type: 1506959623579.002,
        stat: 1506959623579.002 } },
  id: '456',
  type: 'snowmann',
  stat: { '#': 'j8acunbv03sor9v0NeHs7cITj' } }
task { _:
   { '#': 'watcher/789',
     '>':
      { id: 1506959623581.002,
        type: 1506959623581.002,
        stat: 1506959623581.002 } },
  id: '789',
  type: 'moose',
  stat: { '#': 'j8acunbx03sorM0hWZdQz0IyL' } }

在听众方面

const Gun = require('gun');

const gun = new Gun({peers:['http://localhost:8080/gun']});
const tasks = gun.get('tasks');
tasks.map().val(function (task) {
  console.log('task', task);
});

但是,如果其中一个属性更新,则没有结果:

_.forEach(watchers, function (watcher) {
  gun.get(`watcher/${watcher.id}`).put({
    stat: {
      num: 1
    }
  });

为什么?

这是使用https://github.com/sergibondarenko/shared-schedule的代码

4

1 回答 1

1

它没有更新,因为您没有映射该级别。'stat' 已经是一个属性,它是一个对象,所以你不会得到任何改变。

tasks.map().map().val(function (task_stat) {
  console.log('task stat', task_stat);
});
于 2017-10-02T23:12:42.980 回答