0

I'm trying to get some info from an array using web3, when I try this

    async componentDidMount() {
      const product = await store.methods.storeProducts(0).call();
      console.log(product);
      }

everything works fine, it finds the data and everything, but when I try with getInitialProps(), it returns the error " Error: Invalid JSON RPC response: "" ";

  ListProduct.getInitialProps = async () => {
  const product = await store.methods.storeProducts(0).call();
  return {product}
   }

This is the import statements:

  import React, {Component} from 'react';
  import 'semantic-ui-css/semantic.min.css';
  import store from "../ethereum/OnlineStore"

This is what my package.json looks like:

    "@truffle/hdwallet-provider": "^1.4.0",
    "dotenv": "^10.0.0",
    "fs-extra": "^10.0.0",
    "ganache-cli": "^6.12.2",
    "mocha": "^8.4.0",
    "next": "^10.2.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "solc": "^0.8.5",
    "truffle": "^5.3.9",
    "truffle-hdwallet-provider": "^1.0.17",
    "web3": "^1.3.6"
5
  • What's getInitialProps and how is it related to the contract?
    – Ismael
    Jun 19, 2021 at 16:42
  • @Ismael what Im trying to do, is get the data from the storeProducts array, and then access it as a prop, in order to do that i need to use getInitialProps, but I cant seem to make it work
    – Bekim Peci
    Jun 19, 2021 at 16:45
  • How are you using getInitialProps? Is store already initialized?
    – Ismael
    Jun 19, 2021 at 17:22
  • @Ismael I added my getInitalProps and imports to the question, any idea what can be the problem?
    – Bekim Peci
    Jun 19, 2021 at 18:04
  • I don't know much about next but since it work in componentdidmount your contract and abi are fine, the problem can be that web3 hasn't been initialized yet when getinitialprops is called, or it hasn't finished the initialization.
    – Ismael
    Jun 19, 2021 at 21:23

1 Answer 1

0

So, after some days I got back to it, and managed to solve it.

The problem was that the contract I had deployed did not have any data in it.

So, the storeproducts had a length of zero (nothing on it), so I belive that is the reason why it did not work

Second thing that might have done is I got a newer version of Nextjs and Web3

Also recreated the whole project so that might have helped too.

I hope this helps anyone in the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.