Meteor publish subscribe is not reactive

后端 未结 2 1017
日久生厌
日久生厌 2021-01-06 18:55

My client subscription routines are not refreshing when I update a collection:

server/publish.js

Meteor.publish(\'decisions\', funct         


        
相关标签:
2条回答
  • 2021-01-06 19:31

    This (excellent) article might help. To paraphrase:

    ...on the server, Meteor’s reactivity is limited to cursors returned by Meteor.publish() functions. The direct consequence of this is that unlike on the client, code will not magically re-run whenever data changes.

    0 讨论(0)
  • 2021-01-06 19:40

    The callback to Meteor.subscribe is called when the server marks the subscription as ready, and is not a reactive context, so it won't re-run when its dependencies change. (Reactive contexts aren't inherited like closure variables, the fact the callback is physically inside of the autorun is irrelevant.) You probably want a second autorun:

    // original autorun
    Deps.autorun(function() {
      var decSubscription = Meteor.subscribe("decisions", Number(Session.get('decisionCursor')));
      Meteor.subscribe("decisionsToModerate", Number(Session.get('decisionCursor')));
    
      // Store the subscription handle in a session variable so that
      // the new autorun re-runs if we resubscribe
      Session.set("decSubscription", decSubscription);
    });
    
    // new autorun
    Deps.autorun(function() {
      // don't do anything if the subscription isn't ready yet
      var decCursor = Session.get("decSubscription");
      if (!decCursor.ready()) {
        Session.set("decisionsVoted", {});
        return;
      }
      var decisionsVoted = {};
      Decisions.find({active: true}).forEach(/* ... */);
      Session.set("decisionsVoted", decisionsVoted);
    });
    

    Note that we skip computing decisionsVoted if the subscription isn't ready, otherwise when the server is sending the initial result set, we will recompute it after each individual document is sent.

    0 讨论(0)
提交回复
热议问题