2017-05-21 35 views
0

このMeteorサーバーの再帰メソッドcommonHintは、finalResに値があっても、コンソールに未定義のresultを返します。
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 
     } 
    }, 

答えて

1

結果を返す必要があり、結果を返す再帰関数のうちすべてのパス。あなたのパスには、hintersが指定されていない場合と、hinters.length > 0がtrueの場合のパスがあります。

あなたは再帰呼び出しの結果を返す必要があります。

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 
    } 
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);