-1

我正在使用 React 和 AssemblyScript(用于智能合约)在 NEAR 协议上构建一个 Web 应用程序,它们将 NEAR 发送到任何 Near 钱包。我经常收到错误消息:-

Money.jsx:35 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'format')
at submitBtn

每当我尝试发送 Near. 这是我在反应中使用按钮触发的功能:-

if ((checkRecipient === null, checkNEAR === null, checkDetails === null)) {
  alert("Fields are empty");
} else {
  console.log("Recipient");

  // Save transaction

  await window.contract.addTransaction({
    reciever: recipientField.current.value,
    details: detailsField.current.value,
    value: nearField.current.value,
  });

  // Send NEAR

  await window.contract.sendMoney({
    account: recipientField.current.value,
    amount: window.utils.format.parseNearAmount(nearField.current.value),
  });

  alert("Money Sent!");
}

这是 AssemblyScript 中的合同:-

export function sendMoney(account:string, amount:u128,):void{
 ContractPromiseBatch.create(account).transfer(amount)
 logging.log("Money sended successfully to"+ account)
}

即使我收到此错误,交易历史记录也是正确的,我可以看到它们。如果有人帮助我,我会很高兴。

4

1 回答 1

1

似乎formatwindow.utils.format. 从它的外观来看,您可以utils从文件顶部的“near-api-js”导入,并使用它而不是访问窗口:

const { utils } = require("near-api-js");

...
...

  // Send NEAR

  await window.contract.sendMoney({
    account: recipientField.current.value,
    amount: utils.format.parseNearAmount(nearField.current.value), // removed window, use utils from library instead
  });

Near-api-js 文档中还有一个示例,说明如何做到这一点。

于 2022-02-10T13:55:49.900 回答