Meteor secure subscribe&发布

时间:2016-11-25 06:13:40

标签: meteor

此Meteor代码需要发布一个文档,该文档具有由用户输入的字段plateNum匹配值。
 为什么不归还任何东西?如何解决?

// server.publications.js

Meteor.publish('myCol', function (plate) {
  if (this.userId && plate) {
    return myCol.find({plateNum: plate}, {fields: {a: 1, b: 1, c: 1, engineSize: 1 }});
  }
});

字段'a','b','c'的值可能在用户请求时尚未就绪,但将由后端工作人员计算并更新myCol

// client.main.js

Meteor.subscribe('myCol', dict.get('plateNum'));  //<== stored info from user

Template.footer.events({
  'click #info': () => {
    searching = '<span class="note">Searching...</span>';
    let plate = document.getElementById('plateNum').value;
    plate = plate.replace(/\W/g, '').toLowerCase().trim();  //
    dict.set('plateNum', plate);  //<=== store user info here

    let doc = myCol.findOne({plateNum: plate});
    if (!doc || !doc.a) Meteor.call('aaa', plate);
    if (doc && !doc.b) Meteor.call('bbb', {plateNum: plate}, () => {});
    if (doc && doc.c && !doc.c) Meteor.call('ccc', {plateNum: plate}, () => {});
  }
});

1 个答案:

答案 0 :(得分:0)

我想这是因为当您的客户端代码订阅myCol时,this.userId为空,因此服务器不会返回任何内容。

要解决此问题,您需要确保用户在subscribe

之前已经过身份验证

<强>更新

这是您可以用来修复它的代码:

Template.footer.onCreated(function() {
  // use this.autorun over Tracker.autorun
  // so that the code below will be clear on onDestroy event
  this.autorun(function() {
    if(Meteor.userId()) {
      Meteor.subscribe('myCol', dict.get('plateNum'));
    }
  });
});
相关问题