【问题标题】:Meteor: Router with URL of a userName?Meteor:带有用户名 URL 的路由器?
【发布时间】:2016-11-25 04:51:32
【问题描述】:

我在使用用户的用户名创建路由时遇到问题。所以想法是这样的:单击路径并转到该用户配置文件。他的链接应该是这样的:http://www.something.com/usersUsername 我正在阅读并尝试我在互联网上找到的所有关于此的内容,但很多东西都发生了变化,所以我无法管理这个。 我发现唯一有用的是,当页面首先加载客户端 ,,watch" 路径然后订阅一个集合时,我得到了 ,,null" 路径。有什么帮助吗?我的想法是创建一些等待订阅的东西......

包:iron:router、accounts-ui、accounts-password

代码如下:

起始页,模板:

<template name="početna">
<h1>Dobrodošli!</h1>
<h3>Registrujte se:</h3>
    {{> register}}
<h3>Prijavite se:</h3>
    {{> login}}

{{#if currentUser}}
    <h2>Logovan si!</h2>
    {{> logout}}
    <a href="{{pathFor route='profil' username=username }}">Profil</a>
{{/if}}

路由器 JS 文件:

    Router.configure({
    layoutTemplate: 'okvir'
});

// *  * * * *  * //

Router.route('/', {
    name: 'početna', 
    template: 'početna',
});

Router.route('/:username',  {
   waitOn: function(){
       return Meteor.subscribe('userData'), Meteor.user().username
          },    
    name: 'profil',
    template: 'profil',
    
});

简单的 HTML 模板文件,仅用于检查是否有效:

<template name="profil">    
    <h1>RADI</h1>
</template>

谢谢!

【问题讨论】:

  • 您面临的具体问题是什么? Meteor.user().username 的值在 waitOn 或 username 内是否为 null 在 template 内?
  • 在我添加 waitOn 控制台之前,可悲的是 waitOn 中的 null,现在我不知道为什么我的用户名为 null 或如何解决这个问题...

标签: javascript meteor iron-router


【解决方案1】:

给你:

Router.route('/:username',{
    name: 'profil',
    waitOn: function () {
        return Meteor.subscribe('userData', this.params.username)
    },

    action: function () {
        this.render('profil', {
            data: function () {
                return Meteor.users.findOne({username: this.params.username});
            }
        });
    }
});

编辑:

this.params.username 将允许任何人访问该个人资料,无论用户与否。如果你想防止这种情况,你可以使用onBeforeAction()

onBeforeAction: function() {
    if(Meteor.user() && this.params.username == Meteor.user().username){
        this.next();
    } else {
        Router.go('/not-authorized') //or any other route
    }
},

【讨论】:

  • 我仍然收到localhost:3000/null :/ .. 现在唯一的新功能是我在页面上看到“正在加载...”。
【解决方案2】:

卢娜,谢谢你的帮助!露娜的回答有帮助,但我也需要: 1.) 帮助设置用户名=用户名的值

Template["početna"].helpers({ username: function() { return  Meteor.user().username } })

2.) 发布

   Meteor.publish("userData", (username) => {
    return Meteor.users.find({
        username: username
    })
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 2012-06-07
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2015-04-15
    • 2016-07-10
    • 2013-09-05
    相关资源
    最近更新 更多