Return value from recurring function

有些话、适合烂在心里 提交于 2020-01-11 13:13:46

问题


This Meteor server recursive method commonHint returns result undefined to the console even the finalRes has a value.
Any suggestion on how to return the finalRes to the caller? 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:


Every path out of a recursive function that returns a result must return a result. In yours, you have paths that don't: When hinters isn't provided, and when hinters.length > 0 is true.

You should return the result of the recursive call:

  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
  }



回答2:


Any place you call commonHint you need to return the value of the call.

  ... 

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


来源:https://stackoverflow.com/questions/44100349/return-value-from-recurring-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!