简体   繁体   English

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

[英]Creation of user profile page with Meteor and Iron Router

I'm building a web app with Meteor and the Iron Router and one of my goals is to build a profile view for the user that's logged in (where he can edit his info) and profile view for all the users (which can be viewed by any person). 我正在使用Meteor和Iron Router构建一个Web应用程序,我的目标之一是为所有用户(可以编辑他的信息)和所有用户的配置文件视图构建一个配置文件视图(可以查看)任何人)。

The profile view of the user logged in works well, but I'm having problems in creating the user profile view for the other users. 登录用户的配置文件视图运行良好,但我在为其他用户创建用户配置文件视图时遇到问题。 When I try to access directly in the browser to some user profile with the url ( eg: localhost:3000/users/"id" ) it renders the data of the user logged in and the url in the browser changes to localhost: 3000/users/[object%20Object] . 当我尝试直接在浏览器中访问带有url的某个用户配置文件( eg: localhost:3000/users/"id" )时,它会呈现用户登录的数据,并且浏览器中的url更改为localhost: 3000/users/[object%20Object]

Also the tag with the reference to the is always empty when the page with that info is rendered. 此外,当呈现具有该信息的页面时,带有对该引用的引用的标记始终为空。

Here is the code related with this problem: 以下是与此问题相关的代码:

server - publications.js server - publications.js

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

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

User Profile template 用户档案模板

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

User Profile Helpers 用户资料助手

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

Item template 物品模板

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

Item Helpers 项目助手

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

User Profile logged in template 用户配置文件登录模板

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

User Profile logged in helpers 用户配置文件登录了帮助程序

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

Am I missing something? 我错过了什么吗?

Thanks in advance! 提前致谢!

The type of findById that you're getting from Meteor.users.findOne is an Object, and so when it is cast to a string in getProfileUrlById , it ends up being [object Object] . 类型findById是您取得Meteor.users.findOne是一个对象,所以当它被强制转换为一个字符串getProfileUrlById ,它最终被[object Object] You probably want to pass the userId value from the object to that function, not the object itself. 您可能希望将userId值从对象传递给该函数,而不是对象本身。

The primary problem seems to be that you're not publishing private data to the user collection for other users (assuming you have the autopublish package uninstalled). 主要问题似乎是您没有将私有数据发布到其他用户的用户集合(假设您已卸载autopublish包)。 Only fields under profile are published by default by Meteor. Meteor默认只发布profile下的字段。 Your username field for example is not in profile so it will not get published for all users except the one currently logged in. 例如,您的username段不在profile因此除了当前登录的用户之外,不会为所有用户发布。

Secondly, you're overcomplicating route generation. 其次,你过度复杂的路线生成。 Try generating your route like this: 尝试生成这样的路线:

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

This will automatically use the _id field as a key, assuming your router.js file looks like this: 这将自动使用_id字段作为键,假设您的router.js文件如下所示:

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

Your data function for the user_profile route is not returning any data, ie the return is missing. user_profile路由的数据函数未返回任何数据,即缺少返回。 It should be: 它应该是:

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

I am not sure what you want to achieve with this (I suggest to remove it)... 我不确定你想要实现的目标(我建议删除它)......

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

When the route is used the URL should already be the user's profile id (/users/{id}). 使用路由时,URL应该已经是用户的配置文件ID(/ users / {id})。 Besides that you are calling getProfileUrlById with a user object whereas the function expects a user id as string. 除此之外,您使用用户对象调用getProfileUrlById ,而函数需要将用户ID作为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM