This recursive function commonHint
in the Meteor server is causing result
to be undefined in the console, even though finalRes
has a value.
Any ideas on how to properly return finalRes
to the caller? Thanks!
// Calling the recursive method
let result = this.commonHint(myCollection.findOne({age: 44}), shortMatches);
console.log('got most common hint: ' + result); // <=== undefined ====
'commonHint': function (doc, shortMatches, hinters, results = []) {
// For first call when only the first 2 args are defined,
if (!hinters) {
hinters = [...lib.getCombinations(['arg1', 'arg2', 'arg3'], 2, 3)];
this.commonHint(doc, shortMatches, hinters, results); // Hinters is an array of length 3 with 2 elements each
return;
}
// Get an element from hinters, use its 2 hinters and remove that element from the hinters
let hintersToUse = hinters.pop();
let hinter1 = this.cleanMatchItem(hintersToUse[0]);
let hinter2 = this.cleanMatchItem(hintersToUse[1]);
let intersect = _.intersection(hinter1, hinter2);
// Find the item in shortMatches that best matches with the intersection
let tempCol = new Meteor.Collection();
for (let i = 0; i < shortMatches.length; i++) { tempCol.insert({match: shortMatches[i]}); }
results.push(mostSimilarString(tempCol.find({}), 'match', intersect.join(' ')));
if (hinters.length > 0) {
this.commonHint(doc, shortMatches, hinters, results);
} else {
let finalRes = lib.mostCommon(results);
console.log(finalRes); //<==== has a value
return finalRes; //<==== so return it to caller
}
},