Meteor - 添加收藏/类似用户帐户

时间:2016-03-07 01:29:13

标签: meteor

我有一个显示用户列表的页面。我想要当前的用户" is_teacher"能够喜欢'列表中的各个用户。这些喜欢只有老师才能看到。我认为最好的方法是在集合中添加likes: String并存储students_Id,但我的代码必须关闭,因为它不起作用。有人可以查看我做过的事情我会告诉我哪里出错了。

路径:Schemas.js

Schema.UserProfile = new SimpleSchema({
        likes: {
            type: [String],
            optional: true
        }
    });

路径:sudentList.html

<template name="studentList">

    {{#each student}}

    {{#if like}}
    <button class="like">Unlike</button>
    {{else}}
    <button class="like">Like</button>
    {{/if}}

    {{/each}}

</template>

路径:studentList.js

Template.studentList.helpers({
  like: function() {
    return Meteor.users.findOne({likes: this._id});
  }
});

Template.studentList.events({
  'click #like':function(event,template) {
    var student = this._id;
    Meteor.users.update({likes: student}, {$set:{likes:!student}});
  }
});

1 个答案:

答案 0 :(得分:1)

发帖作为答案,因为我无法发表评论。

目前还不清楚是怎么回事,因为你的代码有点混乱,我们需要更多的信息而不是“它不起作用”来诊断问题。

一些明显的观点是:

  • 您的按钮有一个班级(.),但您的活动绑定了一个ID(#
  • 您的更新功能很奇怪。 (至少可以说!)
    • likes是一个数组,因此您不会将$set与字符串
    • 一起使用
    • student是一个字符串,因此!student只是false
    • 据推测,由于您的架构适用于用户个人资料,因此您的查找/更新操作应仅针对profile.likes,而不仅仅是likes

这只是猜测,但我认为你的活动应该更像:

Template.studentList.events({
  'click #like':function(event,template) {
    var student = this._id;
    var teacher = Meteor.userId();
    Meteor.users.update({_id: student}, {$push:{"profile.likes": teacher}});
  }
});

但很难说,因为我不能100%确定你想要做什么。