FindOne永远不会被执行Meteor js

时间:2017-09-26 19:52:32

标签: mongodb publish-subscribe meteor-blaze

我已经有一段时间了。问题是这行代码永远不会被执行let userSchool = SchoolDb.findOne({slug: Session.get('ReceivedSlug')});当我登录控制台时,我看到slug是动态的,因为它假设是从db中提取记录。我该怎么办? oncreated模板

Template.view.onCreated(function () {
    Session.set('ReceivedSlug', FlowRouter.getParam('myslug'));
    this.autorun(function () {
        Meteor.subscribe('SingleSchool', Session.get('ReceivedSlug'));
    });
});

辅助函数

singleSchool: function () {
        if (Meteor.userId()) {
            console.log('reactive this ---- ' +Session.get('ReceivedSlug'));
            let userSchool = SchoolDb.findOne({slug: Session.get('ReceivedSlug')});
            if (!userSchool) {
                Bert.alert('School not present', 'danger', 'growl-top-right');
            } else {
                console.log('school name ----' +userSchool.slug);
                return userSchool;
            }
        }   
    },

1 个答案:

答案 0 :(得分:0)

请检查订阅是否已提取数据。同时在内部控制台发布数据是否在slug更改时发布。

使用以下代码检查订阅是否有效

Meteor.subscribe('SingleSchool', Session.get('ReceivedSlug'), { 
    onReady: function(){
        console.log(SchoolDb.find({}).fetch());
    }
});