发布和订阅单个对象Meteor js [英] Publish and subscribe to a single object Meteor js

查看:85
本文介绍了发布和订阅单个对象Meteor js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,如何发布单个对象似乎还不够清楚.请问什么是处理此问题的最佳方法.此代码段在视图上不显示任何内容.

How to publish single objects seems not clear enough to me. Please what's the best way to handle this. This code snippet does not display anything on the view.

帮助文件

singleSchool: function () {
   if (Meteor.userId()) {
      let myslug = FlowRouter.getParam('myslug');
      var subValues = Meteor.subscribe('SingleSchool', myslug );
        if (myslug ) {
          let Schools = SchoolDb.findOne({slug: myslug});
          if (Schools && subValues.ready()) {
             return Schools;
          }
       }
   }    
},

发布文件

Meteor.publish('SingleSchool', function (schoolSlug) {
  check( schoolSlug, Match.OneOf( String, null, undefined ) );
    user = Meteor.users.findOne({_id:this.userId})
  if(user) {
    if(user.emails[0].verified) {
      return SchoolDb.findOne({ slug: schoolSlug, userId: {$lt: this.userId}});
    } else {
      throw new Meteor.Error('Not authorized');
      return false;
    }
  }
});

模板文件

<template name="view">
  {{#if currentUser}}
    {{#if Template.subscriptionsReady }}
      {{#with singleSchool}}
        {{singleSchool._id}}
        {{singleSchool.addschoolname}}
      {{/with}}
    {{/if}}
  {{/if}}
</template>

推荐答案

正如您所说的" 此代码段在视图上不显示任何内容. " Meteor.publish,您需要返回cursor,而不是array或任何其他object.

As you said "This code snippet does not display anything on the view." well, inside Meteor.publish you need to return cursor, not array or any other object.

因此,请使用以下代码:

So use this code:

Meteor.publish('SingleSchool', function (schoolSlug) {
  check( schoolSlug, Match.OneOf( String, null, undefined ) );
  var user = Meteor.users.findOne({_id:this.userId});
  if(!user || !user.emails[0].verified) {
        throw new Meteor.Error('Not authorized');
    }
    return SchoolDb.find({ slug: schoolSlug, userId: {$lt: this.userId}},{limit:1});
});

我绝对建议您阅读如何避免常见错误

这篇关于发布和订阅单个对象Meteor js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆