繁体   English   中英

NodeJS TypeError:无法读取未定义的属性(读取“0”)

[英]NodeJS TypeError: Cannot read properties of undefined (reading '0')

我从前端得到enemyCards,它是一个数组,有990 x 7 扑克牌。 sortCardOrder function 只需按顺序取出卡片,以便我可以搜索我的数据。

这是我的 NodeJS 代码:

import fs from 'fs';
import G from 'generatorics';

export default function findEnemyStrongest(enemyCards) {
  let eCombination = enemyCards.enemyCards;
  let result = [];
  for(const comb of eCombination){
  result.push(findStrongest(comb));
  }
  console.log(result);
}

function createCombinations(enemyC){
  let combine = enemyC;
  let onlyName = [];
  let allCombinations = [];
  for (let card of combine){
    onlyName.push(card.name);
  }
  for (let comb of G.combination(onlyName, 5)){
    allCombinations.push(comb.slice());
  }
  return allCombinations;
}

function findStrongest(combi){
    let rawdata = fs.readFileSync('data.json');
    let strenghtOrder = JSON.parse(rawdata);
    let combinations = createCombinations(combi);
    let combinationsName = [];
    let ordered = "";
    let result = [];

    for(let combination of combinations){
      let nameString = "";
      let colors = {'C': 0, 'S': 0, 'H':0, 'D':0};
      for(let card of combination){
        nameString += card[0];
        colors[card[1]]+=1
      }
      ordered = sortCardOrder(nameString, colors);
      combinationsName.push(ordered);
      console.log(combinationsName)
      result.push(strenghtOrder.cardStrenght[ordered]);
    }
    return Math.min(...result);
  }

  function sortCardOrder(string, colors){
    }

谁能知道是什么问题?

我们可以推断出这一行是导致错误的原因:

        nameString += card[0];

它正在请求 card 变量的 0 属性,但 card 未定义。 卡片从组合中获得价值。 打印出组合以查看它是否具有未定义的值。

...
for(let combination of combinations){
   console.log(combination)
...

组合从组合中获得价值,组合来自梳子。 打印出梳子的值。

...
for (let comb of G.combination(onlyName, 5)){
   console.log(comb)
...

继续倒退,直到找到“未定义”值的来源。 在没有看到原始源数据(来自 G)的情况下,单步执行代码是找到错误源的唯一方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM