简体   繁体   English

使用流星js使用基于模板的订阅在Mongo Db中显示单个项目

[英]Display single item in Mongo Db using meteor js using template based subscription

I don't know what the problem is, i have this snippet to display single items but it does not work as suppose, What's do i make right? 我不知道问题是什么,我有这个代码片段可以显示单个项目,但不能正常工作,我该怎么办?

publication file: 发布文件:

Meteor.publish('SingleSchool', function (myslug) {
        check(myslug, String);
        if (!this.userId) {
            throw new Meteor.Error('Not authorized');
            return false;
        } else {
            return SchoolDb.find({slug: myslug});
        }
    })

template base subscription: 模板基本订阅:

Template.view.onCreated(function () {
    var instance = this;
    instance.autorun(function () {
        var slug = FlowRouter.getParam('myslug');
        return Meteor.subscribe('SingleSchool', slug);
    });
});

the route: 路线:

FlowRouter.route('/school/:myslug', {
  name: 'view',
  action: function (params) {
    BlazeLayout.render('sidebarschool', {sidebars: 'view'});
  }
})

the template file: 模板文件:

<template name="view">  
    {{#if currentUser}}
    {{#if Template.subscriptionsReady }}
        {{#if SingleSchool}}
            {{#with SingleSchool}}
                <p>{{varibablecalled}}</p>
            {{/with}}
        {{else}}
            <p>Loading...</p>
        {{/if}}
    {{/if}}
</template>

It goes to the slug but no data is displayed for other contents. 进入过程,但未显示其他内容的数据。 The slug in the route works fine. 路线中的弹头工作正常。 用slug路由显示空白页

Did you initialize your SingleSchool collection you're trying to read from in the template? 您是否在模板中初始化了要读取的SingleSchool集合?

Also, don't forget that this is collection , so you should do SingleSchool.findOne({slug:...}) to obtain needed item. 另外,不要忘记这是collection ,因此您应该执行SingleSchool.findOne({slug:...})以获得所需的项目。

Btw, your template file looks like messed up: two <template...> tags. 顺便说一句,您的模板文件看起来像是一团糟:两个<template...>标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM