流星客户端mongodb在变化



Meteor 服务器会导致集合更新,该更新也会更新集合的客户端副本。
有没有办法在更新集合的客户端副本中的记录时调用客户端函数?谢谢

//server
Meteor.users.update({
      _id: Meteor.userId()
    }, {
      $set: {
        'profile.goAhead': true
      });
    //client
    if (Meteor.user().profile.goAhead) {
      myFunc();
    }

你想要 .observeChanges()

let query = MyCollections.find();
query.observeChanges({
  added(id,fields){
    console.log("Document added with _id "+id);
  },
  changed(id,fields){
    console.log("Document with _id "+id+" was changed");
  },
  // ... there are more possible callbacks
});

Meteor 有乐观的更新。首先更新客户端副本。然后在方便的时候更新服务器集合。如果服务器更新有任何问题,则会回滚或使用不同的值更新客户端副本。

在上面的代码中,服务器更新应该包装在 Meteor.methods() 中。从客户端调用服务器方法具有可用于此例的回调方法。

最新更新