使用Meteor和Iron Router创建用户配置文件页面

时间:2013-12-18 20:21:31

标签: javascript meteor meteorite iron-router

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

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

当呈现具有该信息的页面时,带有对该引用的标记始终为空。

以下是与此问题相关的代码:

服务器 - publications.js

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

router.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}
 });

我错过了什么吗?

提前致谢!

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

主要问题似乎是您没有将私有数据发布到其他用户的用户集合(假设您已卸载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'
  });
});

答案 2 :(得分:0)

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作为字符串。