Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从嵌套函数中返回的Javascript_Javascript_Node.js - Fatal编程技术网

从嵌套函数中返回的Javascript

从嵌套函数中返回的Javascript,javascript,node.js,Javascript,Node.js,我正试图返回路由中随机发生器函数的输出。。。我一直在“未定义”-但不知道我做错了什么 var randomizer = function() { // A load of stuff happens here.. and functions that are needed by the pullOut function (I've removed for brevity) var pullOut = function(pick) { if (playerList.length

我正试图返回路由中随机发生器函数的输出。。。我一直在“未定义”-但不知道我做错了什么

var randomizer = function() {
  // A load of stuff happens here.. and functions that are needed by the pullOut function (I've removed for brevity)
  var pullOut = function(pick) {

    if (playerList.length !== pick) {
      var random_item = getRandomItem(list, weight);

      if (playerList.indexOf(random_item) == -1) { // doesn't exist. So add to array.
        playerList.push(random_item);
      }
      pullOut(pick);
    } else {
      console.log(playerList)
      return playerList;
    }
  }
  return pullOut(pick);
}


router.route('/ordercreated')
  .post(function(req, res) {

    var orderedItems = req.body.line_items;
    //  I foreach through all the items - calling the randomizer function on each one...
    _.forEach(orderedItems, function(n) {
      Pack.findOne({
        'product_id': n.variant_id
      }, function(err, pack) {
        if (err) {
          return res.send(err);
        }
        if (pack) {

          var list = [];
          var weight = [];

          _.forEach(pack.playerData, function(n) {
            list.push(n.name);
            weight.push(parseInt(n.chance));
          });


          console.log('randomizing', randomizer(pack.title, list, weight, n.qty, pack.pick));
        }
      });
    });

    res.sendStatus(200);

  })

您的pullOut函数调用自身,但它会丢弃该调用的结果

var randomizer = function() {
  // A load of stuff happens here.. and functions that are needed by the 
  // pullOut function (I've removed for brevity)
    var pullOut = function(pick) {

      if (playerList.length !== pick) {
        var random_item = getRandomItem(list, weight);

        if (playerList.indexOf(random_item) == -1) { // doesn't exist. So add to array.
          playerList.push(random_item);
        }
        return pullOut(pick); // <--- add return
      } else {
        console.log(playerList)
        return playerList;
      }
    }
    return pullOut(pick);
}

如果没有该返回,当函数通过主if语句使用该路径时,它将返回未定义的。

您的pullOut函数调用自身,但它会丢弃该调用的结果

var randomizer = function() {
  // A load of stuff happens here.. and functions that are needed by the 
  // pullOut function (I've removed for brevity)
    var pullOut = function(pick) {

      if (playerList.length !== pick) {
        var random_item = getRandomItem(list, weight);

        if (playerList.indexOf(random_item) == -1) { // doesn't exist. So add to array.
          playerList.push(random_item);
        }
        return pullOut(pick); // <--- add return
      } else {
        console.log(playerList)
        return playerList;
      }
    }
    return pullOut(pick);
}

如果没有该返回,当函数通过主if语句使用该路径时,它将返回未定义的结果。

我真是个笨蛋!现在你指出这一点是显而易见的。谢谢我真是个笨蛋!现在你指出这一点是显而易见的。谢谢