【问题标题】:How can I route to a users profile page?如何路由到用户个人资料页面?
【发布时间】:2016-04-06 16:39:19
【问题描述】:

我展示了一个项目列表,每个项目都属于一个用户。我列出了项目的名称、描述和创建它的用户(按名称)。我还希望有一个指向创建该项目的用户的链接。注意:我还链接到一个项目的页面。

这是我到目前为止所做的:

aldeed:collection2
aldeed:simple-schema
iron:router
aldeed:autoform
useraccounts:iron-routing
accounts-password
accounts-base
useraccounts:core
zimme:active-route
todda00:friendly-slugs
reywood:publish-composite

client/items/listed_item.html

<template name="listedItem">
  <a href="{{pathFor 'itemPage'}}">
    {{name}}
  </a>
  <a href="{{pathFor 'profile'}}">
    {{usernameFromId user}}
  </a>
</template>

客户端/items/items.html

<template name="items">
<div class="items">
        {{#each items}}
            {{> listedItem}}
        {{/each}}
</div>
</template>

client/subscriptions.js

Meteor.subscribe('items');
Meteor.subscribe('allUsernames');
Meteor.subscribe('users');

客户端/helpers.js

Template.items.helpers({
  items: function() {
    return Items.find();
  },
});

Template.registerHelper("usernameFromId", function (userId) {
  var user = Meteor.users.findOne({_id: this.userId});
  return user.profile.name;
});

服务器/publications.js

Meteor.publish("items", function () {
  return Items.find();
});

Meteor.publish("users", function () {
  return Meteor.users.find({}, {
    fields: { profile: 1 }
  });
});

Meteor.publish("allUsernames", function () {
  return Meteor.users.find({}, { 
    fields: { 'profile.name': 1, 'services.github.username': 1  }
  });
});

lib/routes.js

Router.route('/items', function () {
  this.render('items');
});

Router.route('/users/:_id', {
  template: 'profile',
  name: 'profile',
  data: function() {
    return Meteor.users.findOne({_id: this.params._id});
  }
});

这不过是显示用户 URL 的项目 URL,因此它链接到不存在的用户。即items/1234 = users/1234 当系统中只有 users/1 时。如何让它链接到正确的用户 ID?

【问题讨论】:

  • 只是一个问题:在client/helpers.js中你有一个参数userId,但在函数内部你使用this.userId。您是否打算使用 userId 参数查找用户?
  • @JosHarink 是的,没错。我希望那里的助手能够为任何模板查找用户字段。

标签: meteor meteor-blaze meteor-accounts meteor-helper


【解决方案1】:

使用带有构造 URL 的链接

<template name="listedItem">
  <a href="{{pathFor 'itemPage'}}">
    {{name}}
  </a>
  <a href="http://yourdomain.com/users/{{user}}">
    {{usernameFromId user}}
  </a>
</template>

【讨论】:

  • 如果我这样做,我将如何处理使用本地主机?这似乎是硬编码的。
  • 对不起,我写了完整的 URL 是为了清楚它是什么,但你当然可以使用相对路径:&lt;a href="/users/{{user}}"&gt;
  • 错误信息?问题是什么?如果您在浏览器中导航到正确的 url,您会看到您的模板吗?
  • 我看到您的问题是获取正确的用户 ID!所以问题是 item 与 user 有什么关系?您在listedItem 中有userId 吗?我认为您的“用户”变量是正确的用户 ID,但显然这是问题所在。如果用户 ID 在项目对象中,那么您可以使用 this.userID 或它所在的任何对象路径来访问它。如果您需要更多详细信息,请发布您的项目对象的格式
【解决方案2】:

在这部分:

Router.route('/users/:_id', {
  template: 'profile',
  name: 'profile',
  data: function() {
  return Meteor.users.findOne({_id: this.params._id});
 }
});

您已经引用了路由并创建了一个到名为“profile”的模板的路由,但我看不到任何具有该名称的模板。您是否可能只是忘记将其包含在此处?或者你真的没有那个模板?

只是确保。

【讨论】:

    【解决方案3】:

    试试这个:

    Router.route('/users/:_id', {
     template: 'profile',
     name: 'profile'
    }
    

    在帮助器中,您可以通过以下方式检索该记录 id:

    Router.current().params._id
    

    【讨论】:

      【解决方案4】:

      我通常这样使用: 在模板中:

      <div class="css-light-border">
          <a href="/document/{{_id}}">{{title}}</a>
      </div><!-- / class light-border -->
      

      在 router.js 文件中:

      Router.route('/document/:_id', function(){
          Session.set('docid', this.params._id);
          this.render('navbar', {to: "header"});
         this.render('docItem', {to: "main"});
        }); 
      

      这里的 docid 基本上是我要显示的文档的 _id。以下是如何从数据库中提取数据的示例:

      somehelper:function(){
              var currentId = Session.get('docid');
              var product = Products.findOne({_id:currentId});
              return product; 
      

      我认为这是一种简单的方法,因为它使用 Sessions。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 1970-01-01
        • 2014-01-29
        • 1970-01-01
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多