简体   繁体   中英

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?

Also, don't forget that this is collection , so you should do SingleSchool.findOne({slug:...}) to obtain needed item.

Btw, your template file looks like messed up: two <template...> tags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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