How can I determine if the current Meteor.user()._id
exists in the "favoritedBy"
array?
If true, display "Your favorite"
, if false, display "Not your favorite"
.
Here is an example document in MongoDB:
{
"_id" : "W5WwAZatorDEb6DNP",
"createdBy" : "aTmb64zNGSyeDYFJZ",
"favoritedBy" : [
"X594baqWYZiJqA3Qg",
"fgk234m2dkD229d12"
]
}
Assume that Meteor.user()._id
returns X594baqWYZiJqA3Qg
.
How can I perform a true/false check on this?
I attempted the following code:
isFavorite: function() {
var user = Meteor.user()._id;
return Posts.find({favoritedBy: user});
}
In the template:
{{#if isFavorite}}
Your favorite
{{else}}
Not your favorite
{{/if}}
However, the code does not seem to work properly, as it always displays "Your favorite"
, even when the current Meteor user id is not in the array.
Any suggestions?