2016-03-07 71 views
0

我有一个页面,我显示一个列表用户。我希望当前用户“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

张贴作为一个答案,因为我不能发表评论。

由于您的代码有点混乱,我们需要更多信息而不是“无法正常工作”来诊断问题,所以确实不清楚发生了什么。

一些明显的要点是:

  • 你的按钮有一个类(.),但您的事件被绑定到一个ID(#
  • 你更新功能是奇数。 (至少可以这样说!)
    • likes是一个数组,这样你就不会用字符串
    • student使用$set是一个字符串,所以!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%确定你要做什么。