Javascript MeteorJS和Mongo:初始收集计数始终为0

Javascript MeteorJS和Mongo:初始收集计数始终为0,javascript,mongodb,web,meteor,meteor-blaze,Javascript,Mongodb,Web,Meteor,Meteor Blaze,我有一个使用meteor js创建数独网格的自我项目。在这段代码中,我尝试在初始化模板之前先填充数据库,模板将从数据库中读取现有值。下面是我的客户端js代码: Cells = new Mongo.Collection("cells"); if(Meteor.isClient) { Session.setDefault("colMax", 9); Session.setDefault("rowMax", 9); Meteor.startup(function() {

我有一个使用meteor 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;
    }
  });
}
Cells=newmongo.Collection(“Cells”);
if(Meteor.isClient){
Session.setDefault(“colMax”,9);
Session.setDefault(“rowMax”,9);
Meteor.startup(函数(){
对于(var i=0;i
下面是我的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>

数独
{{>创建数独}
重置
{{#每行}
{{>createRow cells=this}
{{/每个}}
{{#每个单元格}
{{>createCell}
{{/每个}}
{{value}}

问题是,每次我刷新页面时,表都会不断增加。要了解更多信息,我打开了自动发布。在这件事上有什么帮助我的提示吗?谢谢

行为是正确的。考虑到实际数据库存储在服务器端,客户端集合的初始计数将始终为0。客户机数据库会随着时间的推移而填充

若要只插入一次某些夹具数据,标准过程是在服务器端执行

然而,若我误解了您的用例,并且您仍然觉得出于某种原因,您必须在客户端执行,然后在subscribe方法的回调中执行

Meteor.subscribe('faq', 
  /* onReady callback */
  function() {
    console.log(somecollection.find().count());
  }
)

请参见此处:

我按照您在客户端代码中的建议添加了Meteor.subscribe(“cells”,function(){…}),但console.log没有对no available做出响应您是否删除了autopublish并添加了Meteor.publish?