无法在meteor mongodb应用程序中显示数据
我有一个基于meteor排行榜示例的测试应用程序,它可以工作,但当我创建自己的应用程序时,我无法显示数据,查询也无法工作。如果我在终端窗口中运行meteor mongo,我可以看到正在插入数据无法在meteor mongodb应用程序中显示数据,meteor,Meteor,我有一个基于meteor排行榜示例的测试应用程序,它可以工作,但当我创建自己的应用程序时,我无法显示数据,查询也无法工作。如果我在终端窗口中运行meteor mongo,我可以看到正在插入数据 db.games.find(); 显示所有游戏 db.games.remove(); 删除所有游戏,但都不能在我的meteor应用程序中运行 此应用程序中除插入内容外,其他内容均无效 if (Games.find().count() === 0) 总是返回零 这是我的main.js文件 var c
db.games.find();
显示所有游戏
db.games.remove();
删除所有游戏,但都不能在我的meteor应用程序中运行
此应用程序中除插入内容外,其他内容均无效
if (Games.find().count() === 0)
总是返回零
这是我的main.js文件
var court = 1;
var currentDate = new Date;
var gameDate = new Date("2014-10-19");
var gameTime = new Date("21:15");
var MinutesSinceMidnightNow = (new Date() - new Date().setHours(0,0,0,0)) / 6000;
var MinutesSinceMidnightGameTime = (new Date().setHours(21,15,0,0) - new Date().setHours(0,0,0,0)) / 6000;
Games = new Meteor.Collection("games");
if (Meteor.isClient) {
Template.game.helpers({
games: function () {
return Games.find({});
} // players func
}); //template helpers
Meteor.startup(function () {
if (Games.find().count() === 0) {
Games.insert({
"court_id": court,
"game_date": gameDate,
"court_name": 'Court 1',
"game_time" : gameTime,
"team_a": "bulldogs",
"team_b": "sea eagles",
"team_a_score": 0,
"team_b_score": 0,
"game_duration": 75,
"game_half": 0,
"game_quarter": 15,
"quarter_time": 3,
"half_time": 5,
"game_minutes": MinutesSinceMidnightGameTime
});
}
});
} // isClient
if (Meteor.isServer) {
Meteor.startup(function () {
Games.remove();
});
}
这是我的HTML文件
<head>
<meta name="viewport" content="width=device-width, user-scalable=no">
</head>
<body>
<div class="outer">
{{> scoresheet}}
</div>
</body>
<template name="scoresheet">
<div class="scoresheet">
{{#each games}}
{{> game}}
{{/each}}
</div>
</template>
<template name="game">
<div class="game">
<h1>"Hello"</h1>
<span class="name">{{game_time}}</span>
<span class="name">{{court_name}}</span>
<span class="name">{{team_a}}</span>
<span class="name">{{team_b}}</span>
<span class="score">{{team_a_score}}</span>
<span class="score">{{team_b_score}}</span>
</div>
</template>
你正在进入比赛状态 query Games.find在声明集合游戏之后但在同步集合游戏之前运行,因此即使n>0,答案也将为0 您可以通过在应用程序完全加载后在控制台中运行Games.find.count来测试情况是否如此。它应该打印n 当您希望集合中有文档时,一个解决方案是将其包装在自动运行中 但对于n=0的情况,这仍然会失败 所以你可以把它放在计时器里测试DDP
然而,在真实的场景中,您应该考虑删除AutoPubLISH包并设置发布/订阅并将代码放在订阅的回调中,如
。您删除了AutoPubLISH包吗?如果是这样,您需要订阅一些数据才能在客户端上看到它。要清空集合,您需要包含选择器{}--这将是游戏。删除{}我刚想到的,将.remove放在服务器启动中也会导致竞争条件。。。你至少应该把它移到正常的服务器部分,但可能会根据我下面的回答添加一个计时器…自动运行没有什么区别。控制台会报告正确的游戏数,但无论数据库中已经有多少游戏,我的应用程序都会插入。我需要客户端测试记录是否已加载,并获取未加载的新记录。最好是,我想在客户端启动时删除一些记录并从APi中刷新它们。@markhorrocks我就是这样做的-您可以在浏览器控制台中公开代码。。。唯一但您不会看到的是,我使用.remove{}将服务器中的集合重置为空method@markhorrocks请参见.js文件的[gist here]。。。Meteor.autorun(function() {
if(Games.findOne()) {
//do your stuff here
}
});
timer = Meteor.setInterval( function(){test();}, 200 );
test = function(){
if(DDP._allSubscriptionsReady()) action(timer);
};
action = function(timer){
Meteor.clearInterval(timer);
/* now do stuff */
console.log('subs are ready, really ready', Games.find().count())
};