I have a collection where currentUser inserts his/her Data.
Template.details.events({
'submit form': function(event) {
event.preventDefault();
var currentUser = Meteor.userId();
var name = event.target.nam.value;
var age = event.target.nombor.value;
var gender = event.target.sex.value;
Data.insert({
name : name,
age: age,
gender: gender,
createdBy: currentUser
});
}
});
Now, I want to show the currentUser a list of people opposite his gender. How do I do it? i am using the helper below right now but this only returns the males. And when I change the selector to "female" it returns the female.
Template.dashBoard.helpers({
genderIs: function(gender){
return this.gender === gender;
},
'people': function(){
return Data.find({gender: "male"});
}
});
I have tried several ways but due to my inexperience none of it is working. here is the HTML
<ul>
{{#each people}}
<li> <a href="#"> {{name}} {{age}} {{gender}} </a> </li>
{{/each}}
</ul>
I have tried out something like this but it didn't work.
{{#if genderIs "female"}}
{{#each people "male"}}
<li> <a href="#">
{{name}} {{age}} {{gender}}
</a>
</li>
{{/each}}
{{else}}
{{#each people "female"}}
<li> <a href="#">
{{name}} {{age}} {{gender}}
</a>
</li>
{{/each}}
{{/if}}
As what the comment said, you could save all the information of user in 'profile' field. So your 'details' template should change to:
Template.details.events({
'submit form': function(event) {
event.preventDefault();
var currentUser = Meteor.userId();
var name = event.target.nam.value;
var age = event.target.nombor.value;
var gender = event.target.sex.value;
Meteor.users.update({ // using update method to update user information
_id: currentUser
},
{
$set:{
"profile.name": name,
"profile.age": age,
"profile.gender": gender
}
});
});
Then, modify your dashboard helpers:
Template.dashBoard.helpers({
genderIs: function(gender){
return Meteor.user().profile.gender === gender;
}, // you may not need this function in your app
'people': function(){
var gender = Meteor.user().profile.gender === "male" ? "female" : "male"
return Data.find({gender: gender});
}
});
But you should pay attention here, if you haven't set the field "profile.gender", it would be undefined and always return "male".
At last, you could using the data return by dashboard.helpers easily:
{{#each people}}
<li><a href="#"> {{profile.name}} {{profile.age}} {{profile.gender}} </a></li>
{{/each}}
I wish it could solve your problem perfectly :-)