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.
It seems like
format
is not available inwindow.utils.format
. From the looks of it, you can importutils
from "near-api-js" in the top of your file, and use it instead of accessing window:There's also an example from the near-api-js docs on how you can do it.