【问题标题】:Creation of user profile page with Meteor and Iron Router使用 Meteor 和 Iron Router 创建用户个人资料页面
【发布时间】:2014-01-07 04:35:51
【问题描述】:

我正在使用 Meteor 和 Iron Router 构建一个网络应用程序,我的目标之一是为登录的用户构建一个配置文件视图(他可以在其中编辑他的信息)和所有用户的配置文件视图(其中任何人都可以查看)。

登录用户的个人资料视图运行良好,但我在为其他用户创建用户个人资料视图时遇到问题。当我尝试直接在浏览器中使用 url (eg: localhost:3000/users/"id") 访问某些用户配置文件时,它会呈现登录用户的数据,并且浏览器中的 url 更改为 localhost:3000/users/[object%20Object]

此外,当呈现包含该信息的页面时,引用 的标记始终为空。

这里是与这个问题相关的代码:

服务器 - publications.js

Meteor.publish('singleUser', function(userId) {
   return Meteor.users.find(userId);
});

路由器.js

Router.map(function() { 
    this.route('profile', {
        path:'/profile',
        data: function() {return Meteor.user();}
    });

    this.route('user_profile', {
        path: '/users/:_id',
        waitOn: function() {
                return Meteor.subscribe('singleUser', this.params._id);
        },
        data: function() { 
                var findById = Meteor.users.findOne(this.params._id);
                if (typeof findById !== "undefined") {
             Router.go(getProfileUrlById(findById),  {replaceState: true});
                }
        }
    }); 
});

用户配置文件模板

<template name="user_profile">
    <h4>Username</h4>
    <p>{{username}}</p>
    <h4>Email:</h4>
    <p>{{email}}</p>    
</template> 

用户配置文件助手

Template.user_profile.helpers({
     username: function() {return Meteor.user().username},
     email: function() {return Meteor.user().emails[0].address} 
});

项目模板

<template name="item">
    </span> <a href="{{profileUrl}}">{{author}}</a>
</template> 

物品助手

Template.item.helpers({
    profileUrl: function() {
        var user = Meteor.users.findOne(this.userId, {reactive:false});
        if(user)
            return getProfileUrlById(user);
    }
 });

getProfileUrlById = function(id) {
    return Meteor.absoluteUrl()+'users/' + id;
}

用户个人资料登录模板

<template name="profile">
    <h4>Username</h4>
    <p>{{username}}</p>
    <h4>Email:</h4>
    <p>{{email}}</p>
</template>

用户个人资料登录助手

Template.profile.helpers({
    username: function() {return Meteor.user().username},
    email: function() {return Meteor.user().emails[0].address}
 });

我错过了什么吗?

提前致谢!

【问题讨论】:

    标签: javascript meteor meteorite iron-router


    【解决方案1】:

    您从Meteor.users.findOne 获得的findById 的类型是一个对象,因此当它被转换为getProfileUrlById 中的字符串时,它最终是[object Object]。您可能希望将 userId 值从对象传递给该函数,而不是对象本身。

    【讨论】:

    • 感谢您的回答。你是在建议这样的事情吗? if (typeof findById !== "undefined") { Router.go(getProfileUrlById(findById.userId), {replaceState: true}); }
    • 你能帮我澄清一下吗?
    【解决方案2】:

    主要问题似乎是您没有将私有数据发布到其他用户的用户集合(假设您已卸载 autopublish 包)。 Meteor 默认只发布profile 下的字段。例如,您的 username 字段不在 profile 中,因此除了当前登录的用户之外,它不会为所有用户发布。

    其次,您使路线生成过于复杂。尝试像这样生成您的路线:

    {{#each users }}
    <a href="{{ pathFor 'userAccount' }}">My account</a>
    {{/each }}
    

    这将自动使用 _id 字段作为键,假设您的 router.js 文件如下所示:

    Router.map(function () {
      this.route('userAccount', {
        path: '/users/:_id'
      });
    });
    

    【讨论】:

      【解决方案3】:

      user_profile 路由的数据函数没有返回任何数据,即缺少返回。应该是:

      data: function() { 
              return Meteor.users.findOne(this.params._id);
            }
      

      我不确定你想用这个实现什么(我建议删除它)......

      if (typeof findById !== "undefined") {
           Router.go(getProfileUrlById(findById),  {replaceState: true});
      }
      

      使用路由时,URL 应该已经是用户的个人资料 ID (/users/{id})。除此之外,您正在使用用户对象调用getProfileUrlById,而该函数需要用户 ID 作为字符串。

      【讨论】:

        猜你喜欢
        • 2014-11-22
        • 1970-01-01
        • 2015-03-06
        • 2019-10-05
        • 1970-01-01
        • 1970-01-01
        • 2012-02-21
        • 1970-01-01
        • 2016-05-13
        相关资源
        最近更新 更多