我正在使用 Meteor 和 Iron Router 构建一个网络应用程序,我的目标之一是为登录的用户构建一个配置文件 View (他可以在其中编辑他的信息)和所有用户的配置文件 View (这任何人都可以查看)。
登录用户的个人资料 View 运行良好,但我在为其他用户创建用户个人资料 View 时遇到问题。当我尝试在浏览器中直接访问某些带有 url 的用户配置文件时(例如:localhost:3000/users/"id"
),它会呈现登录用户的数据和 url浏览器更改为 localhost:3000/users/[object%20Object]
。
此外,当呈现包含该信息的页面时,引用该信息的标记始终为空。
这里是与这个问题相关的代码:
服务器 - publications.js
Meteor.publish('singleUser', function(userId) {
return Meteor.users.find(userId);
});
路由器.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}
});
我错过了什么吗?
提前致谢!
最佳答案
您从 Meteor.users.findOne
获取的 findById
的类型是一个对象,因此当它在 getProfileUrlById 中转换为字符串时
,它最终成为 [object Object]
。您可能希望将 userId
值从对象传递给该函数,而不是对象本身。
关于javascript - 使用 Meteor 和 Iron Router 创建用户配置文件页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20667636/