[Solidity Error] Member "push" is not available in string[] memory outside of storage

2022. 12. 1. 10:51·개인프로젝트/ethFruitShop
728x90
반응형
function getFruitList() public returns(string[] memory){
        string[] memory temp;
        for(uint i=0; i<fruitList.length; i++){
            if(keccak256(abi.encode(fruitList[i])) != keccak256(abi.encode(""))){
                temp.push(fruitList);
            }
            fruitList = temp;
        }
        return fruitList;
    }​

위와 같이 function 안에서 길이를 설정해주지 않은 배열을 선언해주면 동적으로 길이가 늘어나지 않기 때문에 오류가 터졌다.

방법을 찾아보니 해결하는 방법은 두가지가 있다. 

(1) 전역으로 배열 변수를 선언해서 push해주는 방법

(2) function내에서 배열을 선언할때 길이도 같이 선언해주는 방법

나는 후자를 선택했다.

function getFruitList() public returns(string[] memory){
        string[] memory temp = new string[](fruitList.length);
        for(uint i=0; i<fruitList.length; i++){
            if(keccak256(abi.encode(fruitList[i])) != keccak256(abi.encode(""))){
                temp.push(fruitList);
            }
            fruitList = temp;
        }
        return fruitList;
    }

string배열에 배열의 길이를 정해준 새로운 배열을 할당해주면 된다.

728x90
반응형

'개인프로젝트 > ethFruitShop' 카테고리의 다른 글

[ethFruitShop] 중간 점검  (0) 2022.12.05
[React Error] JSON schema for a JavaScript project using TypeScript tooling  (1) 2022.12.02
[ethFruitShop] how to return mapping  (0) 2022.12.01
[Solidity Errror] uncaught (in promise) Error: Please pass numbers as strings or BN objects to avoid precision errors  (0) 2022.12.01
'개인프로젝트/ethFruitShop' 카테고리의 다른 글
  • [ethFruitShop] 중간 점검
  • [React Error] JSON schema for a JavaScript project using TypeScript tooling
  • [ethFruitShop] how to return mapping
  • [Solidity Errror] uncaught (in promise) Error: Please pass numbers as strings or BN objects to avoid precision errors
TeTedo.
TeTedo.
  • TeTedo.
    TeTedo 개발 일기
    TeTedo.
  • 전체
    오늘
    어제
    • 분류 전체보기 (319)
      • 개발 (274)
        • Article (4)
        • 정리 (21)
        • Spring Boot (17)
        • JPA (2)
        • JAVA (6)
        • Database (4)
        • 자료구조 (11)
        • 알고리즘 (32)
        • React (20)
        • Docker (10)
        • node.js (18)
        • Devops (11)
        • Linux (4)
        • TypeScript (3)
        • Go (10)
        • HyperLedger (4)
        • BlockChain (43)
        • html, css, js (48)
        • CS (3)
        • AWS (3)
      • 모아두고 나중에 쓰기 (3)
      • 팀프로젝트 (18)
        • SNS(키보드워리어) (9)
        • close_sea (9)
      • 개인프로젝트 (1)
        • Around Flavor (1)
        • CHAM (13)
        • ethFruitShop (5)
      • 독서 (0)
        • 스프링부트와 AWS로 혼자 구현하는 웹 서비스 (0)
  • 블로그 메뉴

    • 홈
    • 개발일기
    • CS
    • 실습
    • 코딩테스트
    • 웹
    • Go
    • node.js
    • 팀플
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    ERC721
    하이퍼레저
    명령어
    30일챌린지
    mysql
    js
    erc20
    node
    30일 챌린지
    React
    컨테이너
    node.js
    go
    nodejs
    도커
    html
    블록체인
    go언어
    프로그래머스
    CSS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
TeTedo.
[Solidity Error] Member "push" is not available in string[] memory outside of storage
상단으로

티스토리툴바