Solidity

我的契約的奇怪行為

  • March 14, 2022

如您所見,我的契約程式碼:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract GeoSpatialData{

   uint public SpatialDataCount = 0;
   
   struct GeoData{
       uint id;
       address owner_address;
       string data_name;
       string data_year;
       string data_description;
       string data_img_url;
       
       string data_category;
       string data_published_date;
   }

   GeoData[] public geoDatas;

   mapping (uint => GeoData) public geodatas;
   // write function
   function createData(string memory _data_name,address _owner_address,string memory _data_year,string memory  _data_description,string memory _data_image_url,string memory _data_published_date,string memory _data_category,uint _id ) public{
       SpatialDataCount++;
       uint used_id = SpatialDataCount+_id;
       geoDatas.push(GeoData({id: used_id,owner_address:_owner_address,data_name:_data_name,data_year:_data_year,data_description:_data_description,data_img_url:_data_image_url,data_category:_data_category,data_published_date:_data_published_date}));
       
   }
   // read all data  function
   // function fetchAlldata() public view returns(GeoData[] memory){
   //     GeoData[] memory geodatas
   //         return GeoData;
   // }

   // read single data function 
   // function readSingle() public{

   // }

}

uint public SpatialDataCount = 0;

我已將這部分聲明為整數,但是當我嘗試使用 truffle consolse 訪問此契約時,在此處輸入圖像描述 這就是我得到的,我應該只得到一個整數,請幫助今天是我的截止日期

它是一個叫做大數的對象。你可以在這裡查看。如果你想把它轉換成數字,你可以這樣做

value = await x.SpatialDataCount()
value.toNumber()

但是,要小心,因為可以轉換的最大數字大小是 53 位。如果您只想顯示它而不進行任何計算,則可以將其轉換為字元串使用此

value = await x.SpatialDataCount()
value.toString()

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