Solidity

使用 web3js 時獲取 /u0000

  • February 20, 2022

我已經測試了 100% 的工作和返回值,但是當我嘗試使用 javascript 中的 web3 api 讀取數據以完成作業時,我檢索了 /u0000 的返回值作為名稱和符號,我不知道為什麼。

結果

0: ""
1: "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"
name: ""
symbol: "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"
[[Prototype]]: Object

JS

 lookUp: async function (){
   const { lookUptokenIdToStarInfo } = this.meta.methods;
   const id = document.getElementById("lookid").value;
   var star = await lookUptokenIdToStarInfo(id).call();
   console.log(star)
   console.log(JSON.stringify(star));
   App.setStatus("star info is name:" + star.name + " symbol:" +   
   star.symbol + ".");
 }

堅固性

function lookUptokenIdToStarInfo (uint _tokenId) public view returns 
       (string memory name, string memory symbol) {
       //Solidity 0.8.0 and above can return our Star struct but to 
       be pure to previous standard way of doing things we are 
       returning it as a string
       name = tokenIdToStarInfo[_tokenId].name;
       symbol = tokenIdToStarInfo[_tokenId].symbol;
       return (name,symbol);
   }

測試

it('lookUptokenIdToStarInfo test', async() => {
   let tokenId = 7;
   let instance = await StarNotary.deployed();
   await instance.createStar('Awesome Star!', "AS", tokenId, {from: accounts[0]})
   var getByTokenId = await instance.lookUptokenIdToStarInfo(tokenId); 
   assert.equal(await getByTokenId.name, 'Awesome Star!')
   assert.equal(await getByTokenId.symbol, 'AS')
});

所以我通過執行這些步驟解決了這個問題。

  1. 松露編譯
  2. 松露測試
  3. 松露部署
  4. 松露遷移

元遮罩

  1. 設置
  2. 先進的
  3. 重置帳戶

似乎只是重置我的元遮罩帳戶可以解決所有問題。不確定為什麼,但它現在可以工作,我可以檢索數據。我希望這個答案更性感:D

引用自:https://ethereum.stackexchange.com/questions/121991