Howdy.
Probably kind of trivial, but basically, I’m returning the count of documents from a collection that match two values (an ID association and a boolean (active
).
There’s three schemas this is working with. “Run”, “RunChallenge”, and “ChallengeQuestion”.
The template helper:
questionCount: function() {
var runSlug = FlowRouter.getParam('runSlug'),
runId = Run.findOne({
runSlug: runSlug
})._id,
runChallengeId = RunChallenge.findOne({
runChallengeToRun: runId
})._id,
theCount = Meteor.call('countQuestions', runChallengeId, function(error, result) {
return result;
});
console.log(theCount);
}
The server method:
Meteor.methods({
countQuestions: function countQuestions(challengeQuestionTo) {
var questionCount = ChallengeQuestion.find({
challengeQuestionTo: challengeQuestionTo,
active: true
}).count();
var returnedQuestionCount = questionCount.toString();
console.log(returnedQuestionCount + " - " + typeof returnedQuestionCount);
}
});
So, the server console is logging correctly:
I20170410-08:42:30.834(-4)? 4 - string
But the browser console is getting:
Exception in template helper: TypeError: Cannot read property '_id' of undefined
and also the console.log(theCount)
output is returning undefined.
I’ve narrowed down the exception in Template Helper being related to the var runChallengeId
, but can’t seem to figure either of these out.
Is there something I’m doing wrong in practice? I’ve snubbed out similar things like this before to retrieve counts of docs, but not where its crossing relationships.
Suggestions?
Thanks!