2016-11-24 152 views
0

我在用用戶名創建路線時遇到了問題。所以想法是這樣的:點擊路徑並轉到該用戶配置文件。他的鏈接應該是這樣的:http://www.something.com/usersUsername 我正在閱讀並嘗試在互聯網上發現的關於此的所有內容,但很多內容已更改,因此無法管理此內容。 我發現唯一有用的是,當頁面加載客戶端時,首先觀看「路徑,然後訂閱集合,所以我得到了,, null」的路徑。任何幫助?我的想法是創造一些以waitOn用於訂閱...Meteor:具有userName的URL的路由器?

鐵:路由器賬戶的UI賬戶密碼

這裏是代碼:

起始頁,模板:

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

謝謝!

+0

到底是什麼,你所面臨的問題? '''在'模板'裏面的'waitOn'或'username'裏面'Meteor.user().username'的值是否爲null? – Khang

+0

之前,我添加waitOn控制檯傷心,內部waitOn null,現在我不知道爲什麼我在用戶名或如何解決這個問題... –

回答

0

盧娜,感謝您的幫助! Luna的回答幫助,但我還需要: 1)助手來設置用戶名的值=用戶名

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

2)發佈

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

在這裏你去:

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 
    } 
}, 
+0

我仍然得到http:// localhost:3000/null:/ ..現在只有新的,我看到頁面上的「加載...」。 –

相關問題