【问题标题】:With Iron Router, Find() and FindOne()使用 Iron Router、Find() 和 FindOne()
【发布时间】:2016-02-27 08:34:13
【问题描述】:

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

我正在尝试做的事情:在导航栏中,我想列出用户以前的所有条目。我得到了很好的工作。它将每一个都列出到一个下拉列表中,提供一个正确的链接,并且只加载用户之前输入的内容。在正文中,它假设为每个条目加载特定信息。

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

更新:我移动了一些东西并打印了“名称”字段,但仍然无法让它验证所有者。控制台打印出:“模板助手中的异常:ReferenceError: currentCharacter is not defined”替换为当前代码。

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

路线:

  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()})
      };
    }
  });

模板助手类:

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>

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    让我在网上隐喻地大声叹息。像往常一样,当我寻求帮助时,我发现自己做错了什么。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 2016-10-31
      • 2014-08-30
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2023-03-12
      相关资源
      最近更新 更多