使用 Iron Router、Find() 和 FindOne()

With Iron Router, Find() and FindOne()

我用我的 Meteor 加载了 Iron:Router,现在我在加载所有集合和一个特定条目时遇到困难。

我想做什么:在导航栏中,我想列出用户以前的所有条目。我得到了很好的工作。它在下拉列表中列出每一项,提供适当的 link,并仅加载用户之前输入的内容。在正文中,它应该为每个条目加载特定信息。

有什么问题:它要么不给我 findOne() ,其中 params._id 等于路由中的 id,要么它没有加载任何东西。位置是正确的。它只是没有按应有的方式加载信息。

更新:我移动了一些东西并打印了 'name' 字段,但仍然无法让它验证所有者。控制台打印出来:"Exception in template helper: ReferenceError: currentCharacter is not defined" Replacing with current code.

我做错了什么?下面是代码:

路线:

  this.route('character/:_id', {
    template: 'character',
    subscriptions: function() {
      return Meteor.subscribe("characterlist");
      return Meteor.subscribe("characterlist", this.params._id);
    },
    data: function () {
      var routeid = this.params._id;
      return {
        currentCharacter: CharList.findOne({_id: routeid}),
        characterlist: CharList.find({'owner':Meteor.userId()})
      };
    }
  });

模板助手Class:

Template.character.helpers({
    characterlist: function () {
      return CharList.find({}, {sort: {createdAt: -1}});
    },
    currentCharacter: function () {
      return CharList.findOne({'_id':Router.current().params._id});
    },
    isOwner: function () {
      return currentCharacter.owner === Meteor.userId();
    }
  });

HTML:

<template name='character'>

  <div class="container-body">
    {{#if currentUser}}
    <div class="well">
      {{currentCharacter.name}}
      {{#with currentCharacter}}
      {{#if isOwner}}
      <p>Character: {{name}}</p>
      {{else}}
      <p>You are not approved to make spends for this character.</p>
      {{/if}}
      {{/with}}
    </div>
    {{else}}
    <h4>Please log in.</h4>
    {{/if}}

  </div>
</template>

让我在网上比喻性的叹息一声。像往常一样,当我寻求帮助时,我发现我做错了什么。

它需要获取 "this" 对象并拉出 "owner" 类型。一旦它有 "this.owner" 它就可以验证用户当然是正确的所有者。我试图变得聪明,并没有意识到它在四个小时内询问了错误的信息!代码如下:

isOwner: function () {
  return this.owner === Meteor.userId();
}