我正在使用React和AssemblyScript(用于智能合约)在NEAR协议上构建一个Web应用程序,它将NEAR发送到任何Near钱包。我经常收到错误:-
Money.jsx:35 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'format')
at submitBtn
每当我尝试发送附近时。这是我在反应中使用按钮触发的功能:-
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)
}
即使我收到此错误,交易历史记录也是正确的,我可以看到它们。如果有人帮助我,我会很高兴。
似乎format
在window.utils.format
中不可用。从外观上看,您可以从文件顶部的"near-api-js"导入utils
,并使用它而不是访问窗口:
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
});
还有一个来自近 api-js 文档的示例,说明如何做到这一点。