![](/img/trans.png)
[英]how can i connect an address to lunch a function in the smart contract ? | TypeError: Cannot read properties of undefined (reading 'connect') ether.js
[英]Cannot read properties of undefined (reading 'toHexString') in ether.js
如果您使用 JavaScript 并收到此错误,那么您在导入 dotenv 时犯了一个错误。 您正在使用“.config”作为属性。 像下面这样的方法使用它
需要(“dotenv”).config()
它会起作用的。
你很可能搞砸了环境变量。 检查METAMASK_PRIVATE_KEY
是否是正确的名称。 因为否则它将抛出undefined
并且 ethersjs 库之一将尝试在undefined
值上运行hexToString()
方法,因此您会收到错误。
编辑:
您可能会忘记将其包含在您的代码中:
import {} from 'dotenv/config'
// or if its not ESmodule
require('dotenv').config()
当您访问 env 变量时,缺少此导入也会引发undefined
。
您可能遇到的另一个问题是 .env 所在的位置。 确保您的 .env 与您的代码位于同一文件夹中。
我也有这个问题。 我为我的 sol 合同使用了一个构造函数,如下所示:
constructor(
address vrfCoordinatorV2,
uint256 entranceFee,
bytes32 gasLane,
uint64 subscriptionId,
uint32 callbackGasLimit,
uint256 interval
)
经过很多挫折后,我发现我的部署脚本的 arguments 没有按相同的顺序排列。 这导致了错误:
"Cannot read properties of undefined (reading 'toHexString')"
在我的情况下,arguments 需要按以下方式排列:
const args = [
vrfCoordinatorV2Address,
networkConfig[chainId]["raffleEntranceFee"],
networkConfig[chainId]["gasLane"],
subscriptionId,
networkConfig[chainId]["callbackGasLimit"],
networkConfig[chainId]["keepersUpdateInterval"],
];
另一种选择是您编辑了 .env 文件但忘记保存它。 因此脚本不会看到新条目。
我有一个类似的问题,我实际上错过了保存 updated.env 文件。 当我正确保存 updated.env 时,这有效。
args参数中的一个或多个值很可能未定义。
console.log 您的args数组而不部署合约以查看所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.