Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught (in promise) TypeError: Cannot read property 'indexOf' of undefined #292

Closed
memoishin opened this issue Oct 4, 2018 · 13 comments
Closed
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@memoishin
Copy link

I get this error when trying to write data. This error only occurs with some function calls.

Error : Uncaught (in promise) TypeError: Cannot read property 'indexOf' of undefined

Sample Code of where error is encountered:

let contract = new ethers.Contract(process.env.CONTRACT_ADDRESS, process.env.CONTRACT_ABI, 
this.state.web3);
        let privateKey = process.env.PRIVATE_KEY;
        let wallet = new ethers.Wallet(process.env.PRIVATE_KEY, this.state.web3);
        let contractWithSigner = contract.connect(wallet);
        let tx = contractWithSigner.addUpdatedFishData(this.state.tagid, this.state.newTagId, this.state.processType).then((result) => {
          console.log(result);
        });

Is it something to deal with tagid as it is my index in solidity mapping?

@ricmoo
Copy link
Member

ricmoo commented Oct 4, 2018

My guess is that this.state.web3 is a Web3 Provider? You need an ethers.js Provider. The providers.Web3Provider wraps a Web3 Provider. It's a bit confusing. :)

Try:

let provider = new ethers.providers.Web3Provider(this.state.web3);
// Change this to the provider
let contract = new ethers.Contract(process.env.CONTRACT_ADDRESS, process.env.CONTRACT_ABI, provider);
let privateKey = process.env.PRIVATE_KEY;
// Change this to the provider
let wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
let contractWithSigner = contract.connect(wallet);
let tx = contractWithSigner.addUpdatedFishData(this.state.tagid, this.state.newTagId, this.state.processType).then((result) => {
    console.log(result);
});

If this.state.web3 is actually a Web3 object, instead you will need to use:

let provider = new ethers.providers.Web3Provider(this.state.web3.currentProvider);

Let me know if that helps. :)

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Oct 4, 2018
@memoishin
Copy link
Author

Thanks for the response :).

Actually, the provider is ether.js provider. Since the code below was in a helper file, I forgot to mention it in the question.

let abi = process.env.CONTRACT_ABI;
var provider = new Web3.providers.HttpProvider(process.env.LOCAL_PROVIDER) //http://127.0.0.1:7545 is the local provider
let web3Provider = new ethers.providers.Web3Provider(provider);

So this.state.web3 is web3Provider. Since web3 name was already used, I did not change it but rather changed its contents.

The same code works when calling other functions. Also there was another function for which if the tag id was around the value 1000, it worked but with higher values e.g 8000, I got the same error.

I hope my question is clear :)

@ricmoo
Copy link
Member

ricmoo commented Oct 4, 2018

Hmmm... I think I need more context then. :s

Can you include the ABI signature for that call? And can you maybe try running a bunch of values against the tag to see if there is a pattern to which ones work and which do not?

@memoishin
Copy link
Author

This is the ABI for that function.

{
      "constant": false,
      "inputs": [
        {
          "name": "_oldTag",
          "type": "uint256"
        },
        {
          "name": "_newTag",
          "type": "uint256"
        },
        {
          "name": "_processType",
          "type": "uint256"
        }
      ],
      "name": "addUpdatedFishData",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },

Currently this one doesn't work for any values. I tried.

The error is mapped to this place in the ethers.js file (The first if)

case 'sendTransaction':
     return this.send('eth_sendRawTransaction', [params.signedTransaction]).catch(function (error) {
                    // "insufficient funds for gas * price + value"
                    if (error.responseText.indexOf('insufficient funds') > 0) {
                        errors.throwError('insufficient funds', errors.INSUFFICIENT_FUNDS, {});
                    }
                    // "nonce too low"
                    if (error.responseText.indexOf('nonce too low') > 0) {
                        errors.throwError('nonce has already been used', errors.NONCE_EXPIRED, {});
                    }
                    // "replacement transaction underpriced"
                    if (error.responseText.indexOf('replacement transaction underpriced') > 0) {
                        errors.throwError('replacement fee too low', errors.REPLACEMENT_UNDERPRICED, {});
                   }
       throw error;
    });

@ricmoo
Copy link
Member

ricmoo commented Oct 4, 2018

I see, I see. So there was an error getting a response, but for some reason responseText wasn't populated in the error. I should certainly add a check in that block. I will add that tomorrow.

In the mean time, to help you debug, you should be able to just comment out all those if statements. At least you'll be able to march forward. :)

And I'll update this ticket once the new code is live.

@memoishin
Copy link
Author

Ok nice. Thanks for the help :)

@ricmoo ricmoo added bug Verified to be an issue. on-deck This Enhancement or Bug is currently being worked on. and removed discussion Questions, feedback and general information. labels Oct 4, 2018
@memoishin
Copy link
Author

In my case, the actual error was this:

Error: VM Exception while processing transaction: out of gas

How can I set gas limit while calling the contract function to ensure gas doesn't run out while the function is being executed?

@ricmoo
Copy link
Member

ricmoo commented Oct 4, 2018

You can pass in 1 additional parameter, which is a dictionary of overrides, so if foo takes in 2 parameters: contract.foo(param1, param2, { gasLimit: 23000 }). You can override nonce and such as well.

But if you are getting that error, it probably means tour contract is throwing an error, which has the same effect as basically requiring more gas than is allowed.

@memoishin
Copy link
Author

Ok.. Thanks again. Will check the contract :)

@ricmoo
Copy link
Member

ricmoo commented Oct 4, 2018

Also, if you are using truffle, try blowing away the build. Sometimes it updates the ABI but not the bytecode or address, so it lets you call functions that don’t actually exist. :)

@memoishin
Copy link
Author

Yeah I am using truffle. Ok will try that :)

@ricmoo ricmoo removed the on-deck This Enhancement or Bug is currently being worked on. label Oct 5, 2018
@ricmoo
Copy link
Member

ricmoo commented Oct 5, 2018

This should be fixed now, at least as far as nested internal errors shadowing the actual errors. :)

Let me know if you still have problems, or feel free to open another issue to track other problems.

Thanks! :)

@ricmoo ricmoo closed this as completed Oct 5, 2018
@memoishin
Copy link
Author

Thanks :)

asnov added a commit to asnov/ethers.js that referenced this issue Oct 14, 2018
Merge commit '3736a1571480a0f69d632d6fc3bde549cbe46162' into fix/tslib

* commit '3736a1571480a0f69d632d6fc3bde549cbe46162': (41 commits)
  Updated dist files.
  Added automatic event parsing for contract transaction receipts from tx.wait.
  Added ability to wait for a specific number of confirmations (ethers-io#229).
  Fix for geth-etc (official geth is fine), which returns Receipts before the blockHash is synced to the database.
  Fixed confirmations tests and bootstrap fast blockNumber.
  Added confirmations to TransactionResponse (ethers-io#156, ethers-io#238).
  Fixed nested errors for providers that were masking true error (ethers-io#292).
  Updated dist files.
  Added version to errors.
  Fixed French and Spanish for browsers without Uint8Array.forEach.
  Added French and Spanish includes to phantomjs test page.
  Increased timeout for querying npm registry.
  Updated dist files.
  Added French and Spanish wordlist dist files.
  Added French and Spanish BIP-39 wordlists (ethers-io#191).
  Added support for JSON serialized BigNumbers in the constructor (ethers-io#288).
  Fixed scrypt for long passwords (ethers-io#223).
  Updated dist files.
  Added chainId as supported override for contract transactions.
  Fixed wildcard events and made nested events more robust (ethers-io#289).
  ...

Conflicts:
	dist/ethers.min.js
	dist/ethers.min.js.map
	package-lock.json
@ricmoo ricmoo added the fixed/complete This Bug is fixed or Enhancement is complete and published. label Jan 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

2 participants