Help! Something very strange is happening within my app.
I have a merchantReview
template that is supposed to show the number of reviewers.
Below is what the template looks like:
<template name="merchantReview">
<div> {{numberOfReviewers}} Total</div>
And what the helper code looks like:
Template.merchantReview.helpers({
'numberOfReviewers': function () {
var numberOfReviewers = 0;
var merchantProfileId = Session.get('offerUserId2');
console.log("The numberOfReviews _id session is: " + merchantProfileId );
merchantReviews.find({_id: merchantProfileId}).map(function (doc)
{
numberOfReviewers += doc.noOfReviewers;
});
return numberOfReviewers;
}
});
This yields nothing on the merchantReview
page at all.
Lets have a look at the Router:
Router.route('/merchantReview/:_id', {
template: 'merchantReview',
data: function(){
},
waitOn: function(){
return Meteor.subscribe('merchantReviews');
}
});
And what am subscribing to:
Meteor.publish('merchantReviews', function publishmerchantReviews(){
return merchantReviews.find();
});
Now for the "Something very strange" part. When I run this following code (which is similar to the helper code) in the chrome browser console, I get mixed results.
When I run the below:
var numberOfReviewers = 0;
var merchantProfileId = Session.get('offerUserId2');
console.log("The _id is: " + merchantProfileId );
merchantReviews.find({_id: merchantProfileId}).map(function (doc)
{
numberOfReviewers += doc.noOfReviewers;
});
console.log (numberOfReviewers);
...it yields:
The _id is: udEnfEmy5DSBvDsSy
0
As you see, it bypasses the merchantReviews.find
query.
However when slightly alter the code, and run:
var numberOfReviewers = 0;
var merchantProfileId = Session.get('offerUserId2');
console.log("The _id is: " + merchantProfileId );
merchantReviews.find({_id: "udEnfEmy5DSBvDsSy"}).map(function (doc)
{
numberOfReviewers += doc.noOfReviewers;
});
console.log (numberOfReviewers);
This time it yields:
The _id is: udEnfEmy5DSBvDsSy
93
Strange isn't it?
Can anyone explain why the
merchantReviews.find({_id: merchantProfileId})...
query doesn't recognize the merchantProfileId
variable which holds the udEnfEmy5DSBvDsSy
value? And how do I fix this?