Javascript 流星发布/订阅竞赛条件

Javascript 流星发布/订阅竞赛条件,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我有一个定义发布/订阅行为的文件,但它可能只工作10次中的一次。当它不工作时,查询返回为未定义。我可以在chrome的控制台中进行成功的查询,因此必须在查询触发后加载 [项目根目录]/lib/site.js Articles = new Mongo.Collection("articles"); Authors = new Mongo.Collection("authors"); if (Meteor.isServer) { Meteor.publish("articles", fun

我有一个定义发布/订阅行为的文件,但它可能只工作10次中的一次。当它不工作时,查询返回为未定义。我可以在chrome的控制台中进行成功的查询,因此必须在查询触发后加载

[项目根目录]/lib/site.js

Articles = new Mongo.Collection("articles");
Authors = new Mongo.Collection("authors");

if (Meteor.isServer) {
    Meteor.publish("articles", function () {
        return Articles.find();
    });
    Meteor.publish("authors", function () {
        return Authors.find();
    });
}

if (Meteor.isClient) {
    Meteor.subscribe("articles");
    Meteor.subscribe("authors");
}
[项目根]/client/modules/articleAdHelper.js

var getArticle = function (id) {
    var article = Articles.findOne({articleID: id});
    var author = Authors.findOne({_id: article.authorID});
    article.authorName = author.authorName;
    article.authorPageUrl = author.authorPageUrl;
    article.authorAvatar = author.authorAvatar;
    article.articlepublishTimePassedMessage = getPublishDateMessage(article.articlePublishDatetime);
    return article;
};

var getAdUnit = function (unitKey) {
    for (var i = 0; i < unitKey.length; i++) {
        unitKey[i] = getArticle(unitKey[i]);
    }
    return {
        m: [
            unitKey[0],
            unitKey[1]
        ],
        h: [
            unitKey[2],
            unitKey[3],
            unitKey[4],
            unitKey[5]
        ]
    }
};


var articleID = "testarticle";
var adUnitObj = getAdUnit([
    "testarticle",
    "claudeArticle",
    "emusk",
    "claudeArticle",
    "testarticle",
    "adCreatePageTest"
]);
Template.frontPageArticleAdUnit.helpers(adUnitObj);
var getArticle=函数(id){
var article=Articles.findOne({articleID:id});
var author=Authors.findOne({u id:article.authord});
article.authorName=author.authorName;
article.authorPageUrl=author.authorPageUrl;
article.authorAvatar=author.authorAvatar;
article.articlepublishTimePassedMessage=getPublishDateMessage(article.articlePublishDatetime);
退货物品;
};
var getAdUnit=函数(unitKey){
对于(变量i=0;i
基本问题是,您只调用了一次
getAdUnit
,只有在调用时订阅已准备就绪的情况下(如您所发现的),该功能才有效。我不清楚您是否需要非反应性解决方案,因此我将提供两种选择:

非反应性

如果您希望广告单元数据只生成一次(这样广告就不会改变),那么您需要在呈现页面之前等待订阅。如果您使用的是iron router,则需要在路由中添加一个
waitOn
hook

被动的

如果ad单元数据可以更改,则需要为助手返回一个函数,而不是一次调用产生的单个对象。这样,数据将随着作者和文章对客户的可用性而更新

我建议你阅读《流星》中有关使用护卫的内容