You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ethers.js调用合约报invalid BigNumber value错误如何解决?

错误根因分析

报错核心是 ethers.utils.parseUnits 接收的入参为 undefined,无法转为BigNumber类型。你的代码存在变量引用错误:

// 错误写法:定义price变量时,右侧直接引用了还未完成赋值的price变量,必然返回undefined
const price = ethers.utils.parseUnits(price.toString(), 18)
修复步骤
  • 替换parseUnits的入参为你实际要转换的价格原值,从你的注释代码判断你要取的是nft.price,修改为:
    // 转换前先打印确认nft.price有有效值,不是undefined
    console.log('待转换的价格原值:', nft.price)
    // 若nft.price是ETH单位的数值/字符串,用parseUnits转wei
    const price = ethers.utils.parseUnits(nft.price.toString(), 18)
    // 若nft.price本身就是wei单位的数值,直接转BigNumber即可
    // const price = ethers.BigNumber.from(nft.price)
    
  • 确认合约createSale方法的入参顺序和你传参顺序一致,避免入参错位导致price接收到undefined值
  • 若修改后仍报错,在调用contract.createSale前打印price变量确认其已被正确赋值为BigNumber类型

内容的提问来源于stack exchange,提问作者kko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 23:54:01