【问题标题】:Meteor secure subscribe & publishMeteor 安全订阅和发布
【发布时间】:2016-11-25 06:13:40
【问题描述】:

此 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}, () => {});
  }
});

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我猜这是因为在您的客户端代码订阅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'));
        }
      });
    });
    

    【讨论】:

    • 嘿我刚刚更新了答案,如果有任何不清楚的地方请评论,我会解释
    猜你喜欢
    • 2017-11-30
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多