我有一个显示用户列表的页面。我想要当前的用户" 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}});
}
});
答案 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%确定你想要做什么。