2015-05-04 50 views
0

我正在开发一个应用程序,用户可以预订课程,我希望每个用户只能烹饪一次课程。下面是我现在有,希望你们能帮助我:)只允许用户发送一次输入,MeteorJs

'click #book': function(e,tmpl){ 
    if($.inArray(Session.get('userID'), Courses.find({_id: this._id}, {fields:{attendees: 1}})) === -1){ 
    console.log('You have already booked this course'); 
    }else{ 
    Courses.update(this._id, {$push: {attendees: Session.get('userID')}}); 
    Courses.update(this._id, {$inc: {numberPlaces: -1}}); 
    console.log('You have booked the course'); 
    } 

}

正如你可以看到我记录用户的ID在收集并检查他的身份证在已经存在的代码那里每当他试图预订课程。

谢谢!

回答

0

如果在事件处理的上下文是当然的文件,那么你可以这样做:

if (_.contains(this.attendees, Meteor.userid())) 
    console.log('Already booked!'); 

否则,你可以做的检查权的选择:

if (Courses.findOne({_id: this._id, attendees: Meteor.userid()})) 
    console.log('Already booked!'); 

更换Meteor.userid()与你的会话变量,如果这实际上是指一个不同的用户。

+0

谢谢!你的答案真的很有帮助! –