【问题标题】:Meteor: How to subscribe/show usernames when this.userId is publishedMeteor:发布 this.userId 时如何订阅/显示用户名
【发布时间】:2016-04-04 23:03:02
【问题描述】:

注意:完整的源代码可以在这里找到:

https://github.com/Julian-Th/crowducate-platform/

我有以下发布功能:

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;
});

这是模板:

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

订阅助手:

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");
    };

问题:模板不呈现创建的课程。课程创建者无法查看自己的课程或添加其他协作者。我想知道原因是否可能是因为 canEditCourse 现在是一个包含用户名而不是 ID 的数组。

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")
}

通过this 问题,我发现将用户名存储在数组中并不是一个好主意,原因显而易见。一开始,我实际上存储了 ID 而不是用户名,但后来更改了它,因为使用用户名进行自动完成并提前输入并呈现所有协作者的 ID 并不是一件好事。修复它的最佳方法是什么?换句话说,如何渲染所有创建的课程。

【问题讨论】:

  • 我建议你获取 MeteorToys 包(免费版很好)。使用 CTRL + M 在浏览器中查看您的数据。它使调试变得更加容易。

标签: mongodb meteor publish-subscribe subscription


【解决方案1】:

因此,正如您自己回答的那样,您的 Courses 集合字段 canEditCourse 有一个 usernames 数组,而不是 userId,因此查询不会返回任何内容。

【讨论】:

  • 如何将助手更改为:Template.myTeachingCourses.helpers({ 'courses': function() { var myUser = Meteor.user().username; return Courses.find({"canEditCourse": { $in: [ myUser ] } }); } }); 这似乎有效。这样做可以吗?
  • 当然。这是我对您其他问题的回答,但您仍然存在用户名可能可变的问题:如果用户更改他/她的用户名,您的用户名查找将返回新用户名,除非您更新所有课程以替换用户名在数组中,每当用户更改用户名时,用户名都会是旧用户名。这意味着新用户名不会在 Course 对象中找到,并且 Course 不会出现在查询结果中。
  • 如果你想允许按用户名搜索,你真正应该做的是做相反的事情:存储你想通过他们的ID(用户的用户ID)查找的对象,并从用户名查找用户ID。由于用户 ID 是不可变的,因此如果用户名发生更改,它不会影响您的查找:当您搜索用户名的 ID 时,即使用户名已更改,它也始终会找到给定用户的相同 ID。
  • 如果您想通过用户名进行搜索,正如您在某些时候提到的,您可以拥有一个用户对象列表,并将用户名显示为视觉提示,但使用 ID 进行查找。 html 输入列表、下拉列表或此类小部件允许您显示字符串和值:您显示用户名但使用值(在本例中为用户 ID)作为选择。
  • "存储您要通过 ID(用户的用户 ID)查找的对象,并从用户名中查找用户 ID。因为用户 ID 是不可变的,如果用户名发生更改,它不会影响您的查找:"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2017-11-30
  • 2013-11-18
  • 2016-03-07
相关资源
最近更新 更多