3

I have no problem retrieving events with this code:

contract.MyEvent({}, { fromBlock: 0 }).get((err, data) => console.log(data))

But when I use the exact same code in a browser that is using MetaMask, the callback never runs.

Is MetaMask doing something that stops the event from coming back? I can call functions just fine, but I can't retrieve events.

1 Answer 1

5

You should be able to fetch events from in your browser in the same way whether using metamask or using web3 loaded using normal Javascript.

Make sure you're testing using the same node: Getting logs over a wide range can be quite taxing on the node you're pulling from, so not all public nodes will return them before they time out, or at all. If this turns out to be the problem, try reducing the range of the logs you're trying to fetch.

Also make sure web3 is loaded before you start to use it. Typically you want to check it's available after the page onload event, and only start using it then.

Finally, Metamask seems to have problems switching networks cleanly. After switching to the network you want to use, try closing all open Chrome windows (not just the window you're using Metamask in) and then open the browser again from scratch.

3
  • 1
    Quitting my browser worked! Brilliant! It makes sense that that was the issue since there was literally no indication on my local node that anything had happened and metamask didn't return anything (obviously my local contract is not on the network it was connected to). Thank you very much. Commented Oct 19, 2017 at 0:05
  • 2
    There's a current bug in production in MetaMask related to event queries, they're intermittently failing, we're currently working on it.
    – DanF
    Commented Oct 19, 2017 at 0:37
  • @DanF FWIW restarting my browser fixed the issue. LMK if I can help you in any other way Commented Oct 19, 2017 at 15:12

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.