检索所有用户数时流星辅助函数异常

Meteor helper function exception when retrieving the count of all users

在我的网站上,我试图显示注册用户的数量。问题是我在渲染时遇到了一个很长的异常。我的问题是您如何动态而不是静态地执行此操作?

代码在启动时有效,但如果我切换页面并返回,它默认为空白。代码如下:

Template.Home.helpers({
  UserAmount: function() {
    var Count = Meteor.users.find().count();
    document.getElementById("UsersSignedUp").innerHTML = Count;
    console.log("Users-helper: "+Count);
  }
});

以下是主页上的 HTML 部分

<section class="home-main">
  <div class="container">
    <div class="stats">
      <div class="row">
        <div class="col-md-4">
          <label id="UsersSignedUp">{{UserAmount}}</label>
          <span>Users</span>
        </div>
        <div class="col-md-4">
          0
          <span>Listings</span>
        </div>
        <div class="col-md-4">
          0
          <span>Matches</span>
        </div>
      </div>
    </div>
  </div>
</section>

我能够创建一个 onRender 函数来补充错误,但我宁愿不必依赖它。我知道 listings 和 Matches 都设置为 0,但如果我可以修复此错误,其他人将遵循相同的格式。

这是例外情况:

[Log] 模板助手中的异常:UserAmount@http://localhost:3000/Nomad.js?1c729a36a6d466ebe12126c301d77cc72299c832:56:47(meteor.js,第 888 行) http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2880:21 http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1651:21 http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2928:71 _withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:16 http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2927:52 呼叫@http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:172:23 mustacheImpl@http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:109:30 小胡子@http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:113:44 http://localhost:3000/template.Nomad.js?68e12845c8ef55042e39b4867426aab290c3711d:95:30 doRender@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2011:32 http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1865:22 _withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:16 http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1864:54 _withCurrentView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:16 lookup:UserAmount:materialize@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1863:34 _计算@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:327:36 计算@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:243:18 自动运行@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:566:34 自动运行@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1875:29 http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2005:17 无反应@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:13 _materializeView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2004:22 materializeDOMInner@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1476:31 _materializeDOM@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1428:26 http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2040:46 无反应@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:13 _materializeView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2004:22 渲染@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2296:25 插入@http://localhost:3000/packages/iron_dynamic-template.js?d425554c9847e4a80567f8ca55719cd6ae3f2722:522:15 插入@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:1632:22 maybeAutoInsertRouter@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:1622:20

如果我需要提供任何其他信息,请告诉我。

尝试使用它作为助手:

Template.Home.helpers({
  UserAmount: function() {
    return Meteor.users.find().count();
  }
});

想法是这个助手被称为 UserAmount 所以 Home 模板上的值 returns 应该将其自身替换为车把表达式 {{UserAmount}}

您不必自己动手更改 DOM。 Meteor 可能对为什么 dom 在尝试更改时发生更改感到困惑,但它放弃了该错误。