2015-05-04 51 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

謝謝!你的答案真的很有幫助! –