注销后无法查看用户配置文件(Meteor& Iron路由器) [英] Unable to view user profiles when logged out (Meteor&Iron Router)

查看:82
本文介绍了注销后无法查看用户配置文件(Meteor& Iron路由器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为正在开发的应用程序构建了一个用户个人资料页面,当用户登录时该页面可以正常工作,但是如果没有人登录,则模板为空.我的目标是任何人(即使未在应用程序中注册)都可以查看用户个人资料.

I've built a user profile page for an app that I'm developing, the page works fine when an user is logged in, but when nobody is logged in the templates are empty. My goal is that anyone (even if it isn't registered in the app) is able to see the user profiles.

这是代码:

出版物:

Meteor.publish('singleUser', function(userId) {
    if (this.userId) {
        var findById = Meteor.users.find(userId);
        return findById.count() ? findById : 'undefined';
    }
    return [];
 });

路由器:

this.route('user_profile', {
    path: '/users/:_id',
    waitOn: function() {
        return Meteor.subscribe('singleUser', this.params._id);
    },
    data: function() { 
        return Meteor.users.findOne({_id: this.params._id}); 
    }
});

个人资料模板:

<template name="user_profile">
    <h4>Username</h4>
    <p>{{username}}</p>
    <h4>User since:</h4>
    <p>{{createdAtFormatted}}</p>                   
</template> 

个人资料助手:

Template.user_profile.helpers({
    createdAtFormatted: function(){
        return  moment(this.createdAt).fromNow();
    }   
});

我不知道代码中缺少什么.

I don't know what's missing in my code.

谢谢!

推荐答案

您几乎明白了-您只需要修复发布功能即可.您导航到个人资料页面时仅发布必要用户的方法是正确的.

You almost got it - you just need to fix the publish function. Your approach of only publishing the necessary user when you navigate to the profile page is correct.

在发布功能中,this.userId引用主叫用户的ID.由于尚未登录的客户端没有userId,因此您的发布功能将返回[]且该客户端将无法呈现配置文件页面,这是有原因的.其余的发布功能是不必要的-它应该返回一个游标,而不必处理找不到数据的所有可能性.我想您想要这样的东西:

Inside of a publish function, this.userId refers to the calling user's id. Because a client which hasn't logged in does not have a userId, it stands to reason that your publish function will return [] and the client will be unable to render the profile page. The rest of your publish function is unnecessary - it should return a cursor and not have to handle every possibility of data not being found. I think you want something like this:

Meteor.publish('userForProfilePage', function(userId) {
  check(userId, String);
  return Meteor.users.find(userId, {
    fields: {createdAt: 1, username: 1}
  });
});

请注意以下几点:

  • 我为该函数使用了一个明确的名称,以清楚地标识我在做什么.我发现userForProfilePagesingleUser更清晰,但这是一个品味问题.请确保适当更改您的订阅.
  • 我在userId输入上使用了简单的检查.这将验证它是一个字符串并且不是未定义的.您可以根据需要添加更多复杂的检查.
  • 仅当支票通过时,我们才会返回userId的游标.游标返回的文档将仅包含_idcreatedAtusername字段(这样做是为了确保安全).
  • I used an explicit name for the function to clearly identify what I'm doing. I find userForProfilePage more clear than singleUser, but this is a matter of taste. Be sure to change your subscribe as appropriate.
  • I used a simple check on the userId input. This validates that it is a string and not undefined. You can add more sophisticated checks as needed.
  • Only if the check passed, will we return a cursor for the userId. The documents returned by the cursor will only contain the _id, createdAt, and username fields (this is done for security).

这篇关于注销后无法查看用户配置文件(Meteor&amp; Iron路由器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆