web3js报错: TypeError: Cannot read properties of undefined (reading 'forEach')


web3js报错: TypeError: Cannot read properties of undefined (reading 'forEach')

在fork一个跨链项目前端,做多链环境(tron,bac,heco)相互切换的时候,由于涉及到全局变量的重新注册,发现了一个问题,在每次重新实例化web3合约对象的时候,都会遇到这个报错:
TypeError: Cannot read properties of undefined (reading 'forEach')

而在页面第一次载入的时候实例化相同的代码参数却不会有这个问题。在排除了参数和对象的问题之后,求助于google,看到在web3的github中有人提出了相同的issue:点击查看
在这里官方提到了是abi的问题,已经在最新版本中修复:

查看最新issue

由于我本人遇到的情况是在erc20合约实例化时的报错。想了想,目前的这个项目的框架好像确实是我一两年前搭建的,然后被同事复制粘贴缝缝补补用到了现在(尴尬)。。。所以就尝试更新我的erc20 token 的 abi。
在更换abi完毕之后,问题解决。
总结: 可以看到这个问题归根结底还是使用了一些过旧的库/资源。公司的一些前端项目框架其实是我很久很久之前第一次接触链的项目的时候摸着石头过河垒起来的,然后后来者就也摸着同样的石头过了同样的河,久而久之,同事之间相互复制粘贴(包括本人),使得框架就像漂摇风雨中的老房子一样,摇摇欲坠。对于一个技术人员来说,往往面临着更新自己的技术栈与deadline的冲突,能在工作中完成自身的技术升级最好不过,但是如果两者相较,还是要首先保证业务完成,把技术栈的自我学习放在工作之余了(首先维护好一个屎山自己再建立自己的屎山)。毕竟只靠工作时间的提升也是有限的。
在这里,不禁赋诗一首
项目诚可贵
deadline 更高
若为上线故
二者皆可抛。
最后附上我的最新erc20 abi:
[
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": true,
          "internalType": "address",
          "name": "owner",
          "type": "address"
        },
        {
          "indexed": true,
          "internalType": "address",
          "name": "spender",
          "type": "address"
        },
        {
          "indexed": false,
          "internalType": "uint256",
          "name": "value",
          "type": "uint256"
        }
      ],
      "name": "Approval",
      "type": "event"
    },
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": true,
          "internalType": "address",
          "name": "from",
          "type": "address"
        },
        {
          "indexed": true,
          "internalType": "address",
          "name": "to",
          "type": "address"
        },
        {
          "indexed": false,
          "internalType": "uint256",
          "name": "value",
          "type": "uint256"
        }
      ],
      "name": "Transfer",
      "type": "event"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "owner",
          "type": "address"
        },
        {
          "internalType": "address",
          "name": "spender",
          "type": "address"
        }
      ],
      "name": "allowance",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "spender",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "value",
          "type": "uint256"
        }
      ],
      "name": "approve",
      "outputs": [
        {
          "internalType": "bool",
          "name": "",
          "type": "bool"
        }
      ],
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "who",
          "type": "address"
        }
      ],
      "name": "balanceOf",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "decimals",
      "outputs": [
        {
          "internalType": "uint8",
          "name": "",
          "type": "uint8"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "name",
      "outputs": [
        {
          "internalType": "string",
          "name": "",
          "type": "string"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "symbol",
      "outputs": [
        {
          "internalType": "string",
          "name": "",
          "type": "string"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "totalSupply",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "to",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "value",
          "type": "uint256"
        }
      ],
      "name": "transfer",
      "outputs": [
        {
          "internalType": "bool",
          "name": "",
          "type": "bool"
        }
      ],
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "from",
          "type": "address"
        },
        {
          "internalType": "address",
          "name": "to",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "value",
          "type": "uint256"
        }
      ],
      "name": "transferFrom",
      "outputs": [
        {
          "internalType": "bool",
          "name": "",
          "type": "bool"
        }
      ],
      "stateMutability": "nonpayable",
      "type": "function"
    }
  ]