[英]Creation of user profile page with Meteor and Iron Router
我正在使用Meteor和Iron Router構建一個Web應用程序,我的目標之一是為所有用戶(可以編輯他的信息)和所有用戶的配置文件視圖構建一個配置文件視圖(可以查看)任何人)。
登錄用戶的配置文件視圖運行良好,但我在為其他用戶創建用戶配置文件視圖時遇到問題。 當我嘗試直接在瀏覽器中訪問帶有url的某個用戶配置文件( eg: localhost:3000/users/"id"
)時,它會呈現用戶登錄的數據,並且瀏覽器中的url更改為localhost: 3000/users/[object%20Object]
。
此外,當呈現具有該信息的頁面時,帶有對該引用的引用的標記始終為空。
以下是與此問題相關的代碼:
server - 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}
});
我錯過了什么嗎?
提前致謝!
類型findById
是您取得Meteor.users.findOne
是一個對象,所以當它被強制轉換為一個字符串getProfileUrlById
,它最終被[object Object]
。 您可能希望將userId
值從對象傳遞給該函數,而不是對象本身。
主要問題似乎是您沒有將私有數據發布到其他用戶的用戶集合(假設您已卸載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'
});
});
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作為字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.