2016-03-07 70 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%確定你要做什麼。