TypeError: Cannot read properties of undefined (reading 'format')

1.8k Views Asked by At

I am building a web app on NEAR Protocol using React and AssemblyScript (for smart contracts), which send NEAR to any Near wallet. I am constantly getting an error:-

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

Whenever I try to send Near. Here is the function I am triggering using a button in react:-

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!");
}

Here is the contract in AssemblyScript:-

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

Even if I am getting this error, it's right the transaction history and I can see them. I will be glad if someone helps me.

1

There are 1 best solutions below

0
On

It seems like format is not available in window.utils.format. From the looks of it, you can import utils from "near-api-js" in the top of your file, and use it instead of accessing window:

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
  });

There's also an example from the near-api-js docs on how you can do it.