2016-03-08 98 views
1

我試圖搜索陣列以找到this._idstudentId returns正確的ID但是fav returnsObject {_id: "fMNS3M5dW356C46Sq", emails: Array[1], profile: Object, roles: Array[2]}在陣列中搜索特定值

我怎麼var fav返回數組中的studentId或者如果ID沒有找到返回null?

路徑:基於您的評論list.js

Template.list.events({ 
'click .favourite':function(event,template) { 
     console.log('click'); 
     var studentId = this._id; 
     var fav = Meteor.users.findOne({"profile.favorites": studentId }); 

     console.log(fav); 

     if (candidateId === fav) { 
      Meteor.users.update({_id: Meteor.userId()}, {$pull:{"profile.favorites":studentId}}); 
     } else { 
      Meteor.users.update({_id: Meteor.userId()}, {$push:{"profile.favorites":studentId}}); 
     }; 
    } 
}); 
+0

如果findOne成功返回用戶,是否要求等於studentId? –

+0

是的,如果findOne找到相同的userId,那麼fav應該等於studentId – bp123

回答

1

,我想你想:

Template.list.events({ 
    'click .favourite':function(event,template) { 
    console.log('click'); 
    var studentId = this._id; 
    var fav = null; 

    if(Meteor.users.findOne({"profile.favorites": studentId })) { 
     fav = studentId; 
    } 
    if (candidateId === fav) { 
     Meteor.users.update({_id: Meteor.userId()}, {$pull:{"profile.favorites":studentId}}); 
    } else { 
     Meteor.users.update({_id: Meteor.userId()}, {$push:{"profile.favorites":studentId}}); 
    }; 
    } 
}); 

這將設置最愛是studentId如果有什麼Meteor.users.findOne回報。

+0

是的寶貝!你是炸彈隊友! – bp123