【问题标题】:Meteor. Problems with subscribe/publish流星。订阅/发布问题
【发布时间】:2015-09-11 18:17:49
【问题描述】:

我有问题。 我正在尝试构建 highcharts 图形。 这个怎么运作: 我要去我的路线('ship.details'),在这里我没有问题。 我的问题: 订阅(ships_snapshots_all)不起作用。 我的 publish.js:

Meteor.publish("ships_snapshots", function(user, options) {
    if(!this.userId) return null;
    if(this.userId) {
        console.log('subsribed by ' + user);
        return ships_snapshots.find({userId: user}, options);
    }
});
Meteor.publish("ships_snapshots_all", function() {
    return ships_snapshots.find({});
})

我的 subscribe.js(在 lib 文件夹中):

Meteor.subscribe('ships_snapshots');
Meteor.subscribe('ships_snapshots_all');

问题 100% 在我的订阅中,因为如果我正在安装 autopublish,一切正常。我认为我的路由器有问题。

router.js:

Router.route('/ships/details', {
    name: 'ship.details',
    loadingTemplate: 'loading',
    onBeforeAction: function() {
        var shipId = Session.get('currentShipId');
        if(!shipId) {
            Router.go('user.ships');
        } else {
            this.next();
        }
    },
    waitOn: function() {

        if (Meteor.isClient) {
        var getCompare = Meteor.user().profile.wows.compareWith;
        console.log(getCompare);
        var user2 = Meteor.users.findOne({"profile.wows.nickname": getCompare});
        var user2Id = user2._id;
        if (getCompare) {
            var user2 = Meteor.users.findOne({"profile.wows.nickname": getCompare});
            if (user2) {
                var user2Id = user2._id;
            }
        }
        if (getCompare) {
            var handle = Meteor.subscribe('ships_snapshots', Meteor.user()._id) && Meteor.subscribe('ships_snapshots', user2Id) && Meteor.subscribe('userSearchInfo', getCompare);
            Session.set('compareWith', user2);
            console.log('user2 _____');
            console.log(user2);
            return handle
        } else {
            var handle = Meteor.subscribe('ships_snapshots', Meteor.user()._id) && Meteor.subscribe('ships_snapshots', user2Id);
            return handle
        }
    }, data: function() {
            if (handle.ready()) {
            var shipname = this.params.shipName;
            var obj = {};

            var query = ships.findOne(); 
            var shipId = Session.get('currentShipId');
            var result;
            _.each(Meteor.user().profile.wows.ships, function(row) {
                if (row.ship_id === shipId) {
                    result = row;
                }
            });     
            return result;
        }
    }
});

我认为我的问题在于订阅 ship_snapshots。这里出了点问题,但我无法解决这个问题。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    “不工作”到底是什么意思?从您的代码中,我假设您总是看到所有的船舶快照。

    如果您的路由器中有订阅,您不应该在 /lib 中有订阅。如果您在 /lib 中有 Meteor.subscribe('ships_snapshots_all');,那么您应该始终看到所有船舶快照(假设您没有在任何地方停止该订阅)。

    您对所有人的订阅也应该是:

    Meteor.publish("ships_snapshots", function(user, options) {
        if(this.userId) {
            console.log('subsribed by ' + user);
            return ships_snapshots.find({userId: user}, options);
        } else this.ready();
    });
    

    你不想在没有用户的情况下返回null,你可以将订阅标记为准备好而不找到任何记录。这不是您的问题的原因,而只是良好的做法。

    【讨论】:

    • 谢谢你,Michel,你的回答对我很有帮助,我发现了主要问题:我没有正确订阅 user2 信息。
    【解决方案2】:
    Meteor.publish("ships_snapshots", function(user, options) {
        if(!this.userId) return null;
        if(this.userId) {
            console.log('subsribed by ' + user);
            return ships_snapshots.find({userId: user._id}, options);
        }
    });
    

    在您的发布脚本中,用户真的是一个 id 还是一个用户对象?我将其更改为 user._id。请检查一下。

    【讨论】:

    • 感谢您的回复。如您所见,在 router.js 中,我将 user2Id 变量(=== user2._id)传递给发布函数,所以问题不在这里。
    • 您的 ship_snapshots 选项始终未定义。是故意的吗?
    • 在我的图形中,我需要获取第二个用户的数据以与他比较一些数据,所以我尝试在 router.js 中进行订阅(但它不起作用),所以我'我刚刚添加 Meteor.subscribe('ships_snapshots_all') (这会发布所有数据); subscribe.js,但它仍然无法正常工作..
    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 2015-05-15
    • 1970-01-01
    • 2018-12-18
    • 2018-12-26
    相关资源
    最近更新 更多