![](/img/trans.png)
[英]How to display Mongo DB collection in client main view in MeteorJS?
[英]MeteorJS and Mongo: Initial collection count is always 0
我有一个使用流星js创建数独网格的自我项目。 在这段代码中,我尝试在初始化模板之前先填充数据库,然后模板将从数据库中读取现有值。 以下是我的客户端js代码:
Cells = new Mongo.Collection("cells");
if(Meteor.isClient) {
Session.setDefault("colMax", 9);
Session.setDefault("rowMax", 9);
Meteor.startup(function() {
for(var i = 0; i < Session.get("rowMax"); i++) {
for(var j = 0; j < Session.get("colMax"); j++) {
if(Cells.find({row: i, col: j}).count() == 0) {
Cells.insert({
value: -1,
row: i,
col: j,
createdAt: new Date()
});
}
}
}
});
Template.createSudoku.helpers({
rows: function() {
var _rows = [];
for(var i = 0; i < Session.get("rowMax"); i++) {
_rows.push(Cells.find({row: i}, {sort: {col: 1}}));
}
return _rows;
}
});
}
下面是我的html代码
<body>
<header>
<h1>Sudoku</h1>
</header>
<table class="sudoku">
{{> createSudoku}}
</table>
<button class="reset">reset</button>
</body>
<template name="createSudoku">
{{#each rows}}
{{> createRow cells=this}}
{{/each}}
</template>
<template name="createRow">
<tr>
{{#each cells}}
{{> createCell}}
{{/each}}
</tr>
</template>
<template name="createCell">
<td class="cell-{{row}}-{{col}}">{{value}}</td>
</template>
问题是,每次刷新页面时,表格都会不断增加。 有关其他信息,我打开了自动发布。 有什么建议可以帮助我吗? 谢谢!
行为是正确的。 假定实际的数据库存储在服务器端,则客户端收集的初始计数将始终为0。 随着时间的推移,将填充客户端数据库。
如果只想插入一些灯具数据,则标准程序是在服务器端进行。
但是,如果我误解了您的用例,但您仍然出于某种原因仍然感到这是必须在客户端执行的操作,则可以在您的subscription方法的回调中进行操作。
Meteor.subscribe('faq',
/* onReady callback */
function() {
console.log(somecollection.find().count());
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.