Meteor:发布 this.userId 时如何订阅/显示用户名 [英] Meteor: How to subscribe/show usernames when this.userId is published
问题描述
注意:可以在此处找到完整的源代码:
https://github.com/Julian-Th/crowducate-platform/一个>
我有以下发布功能:
Meteor.publish('editableCourses', function () {return Courses.find({"canEditCourse": { $in: [ this.userId ] } });});Meteor.publish("userData", function () {如果(this.userId){返回 Meteor.users.find({_id: this.userId},{fields: {'realname': 1, 'username': 1, 'gender': 1, 'language': 1, 'biography': 1 }})}});Meteor.publish("allUsernamesExceptCurrent", function () {//获取当前用户var currentUserId = this.userId;//获取除当前用户外的所有用户,//只获取用户名字段var users = Meteor.users.find({_id: {$ne: currentUserId}},{字段:{用户名:1}});回访用户;});
这是模板:
<button class="btn btn-primary js-create-course"><i class="fa fa-plus"></i>创建课程<小时><div class="row"><ul class="thumbnails list-unstyled">{{#每个课程}}{{>课程卡}}{{/每个}}
模板>
订阅助手:
Template.myTeachingCourses.helpers({课程":函数(){返回 Courses.find();}});Template.myTeachingCourses.created = function () {//获取模板实例的引用var 实例 = 这个;//订阅所有已发布的课程instance.subscribe("editableCourses");};
问题:模板不呈现创建的课程.课程创建者无法查看自己的课程或添加其他协作者.我想知道原因是否可能是因为 canEditCourse 现在是一个包含用户名而不是 ID 的数组.
我的课程 JSON:{"_id": "hRX8YABpubfZ4mps8","title": "这里的课程名称","coverImageId": "Qojwbi2hcP2KqsHEA","作者": "姓名",关键词":[测试",流星"],已发布":真实","about" : "测试",可以编辑课程":[用户 1"],"createdById" : "zBn6vtufK2DgrHkxG",日期创建":ISODate(2015-12-29T21:42:46.936Z")}
通过这个 问题 我发现将用户名存储在数组中并不是一个好主意,原因很明显.一开始,我实际上存储了 ID 而不是用户名,但后来更改了它,因为使用用户名进行自动完成和预先输入并呈现所有合作者的 ID 并不是一件好事.修复它的最佳方法是什么?换句话说,如何呈现所有创建的课程.
因此,当您自己回答时,您的课程集合字段 canEditCourse
有一个 usernames
数组,而不是userId
,因此查询不会返回任何内容.
Note: Whole sourcecode can be found here:
https://github.com/Julian-Th/crowducate-platform/
I have the following pub functions:
Meteor.publish('editableCourses', function () {
return Courses.find({"canEditCourse": { $in: [ this.userId ] } });
});
Meteor.publish("userData", function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId},
{fields: {'realname': 1, 'username': 1, 'gender': 1, 'language': 1, 'biography': 1 }})
}
});
Meteor.publish("allUsernamesExceptCurrent", function () {
// Get current user
var currentUserId = this.userId;
// Get all users except current,
// only get username field
var users = Meteor.users.find(
{_id: {$ne: currentUserId}},
{fields: {username: 1}}
);
return users;
});
Here's the template:
<template name="myTeachingCourses">
<button class="btn btn-primary js-create-course"><i class="fa fa-plus"></i> Create a Course</button>
<hr>
<div class="row">
<ul class="thumbnails list-unstyled">
{{#each courses}}
{{>courseCard}}
{{/each}}
</ul>
</div>
</template>
The helper with subscription:
Template.myTeachingCourses.helpers({
'courses': function(){
return Courses.find();
}
});
Template.myTeachingCourses.created = function () {
// Get reference to template instance
var instance = this;
// Subscribe to all published courses
instance.subscribe("editableCourses");
};
The problem: The template doesn't render the created courses. A course creator can't see his own courses or add other collaborators. I wonder if the reason might be, because canEditCourse is now an array with usernames and not with IDs.
My JSON for Courses:
{
"_id" : "hRX8YABpubfZ4mps8",
"title" : "Course Title here",
"coverImageId" : "Qojwbi2hcP2KqsHEA",
"author" : "Name",
"keywords" : [
"test",
"meteor"
],
"published" : "true",
"about" : "Testing",
"canEditCourse" : [
"User1"
],
"createdById" : "zBn6vtufK2DgrHkxG",
"dateCreated" : ISODate("2015-12-29T21:42:46.936Z")
}
Via this question I found out that storing usernames in the array is not a good idea for now obvious reasons. In the beginning, I actually stored IDs instead of usernames but then changed it because to use the username for autocomplete with typeahead and rendering the IDs of all collaborators is not good thing to do. What is the best way to fix it? In other words, how is it possible to render all created courses.
So, as you answered yourself, your Courses collection field canEditCourse
has an array of usernames
, not userId
, so the query will not return anything.
这篇关于Meteor:发布 this.userId 时如何订阅/显示用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!