即使commonHint
有值,此Meteor服务器递归方法result
也会向控制台返回finalRes
未定义。
有关如何将finalRes
返回给来电者的任何建议? THX
//call 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 = []) {
// first call only 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);
// which item of the shortMatches best matches with the intersect
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
}
},
答案 0 :(得分:1)
您呼叫commonHint
的任何地方都需要返回通话值。
...
if (!hinters) {
hinters = [...lib.getCombinations(['arg1', 'arg2', 'arg3'], 2, 3)];
return this.commonHint(doc, shortMatches, hinters, results); // hinters is an array of length 3 with 2 elements each
}
...
if (hinters.length > 0) {
return this.commonHint(doc, shortMatches, hinters, results);
答案 1 :(得分:1)
返回结果的递归函数中的每个路径必须返回结果。在您的网站中,您的路径不是:未提供hinters
时,hinters.length > 0
为真。
你应该返回递归调用的结果:
if (!hinters) {
hinters = [...lib.getCombinations(['arg1', 'arg2', 'arg3'], 2, 3)];
return this.commonHint(doc, shortMatches, hinters, results); // hinters is an array of length 3 with 2 elements each
// ^^^^^^
}
// ...
if (hinters.length > 0) {
return 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
}