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

TypeError: Cannot read property 'on' of undefined, can't access event from nodejs #958

Closed
stiv-yakovenko opened this issue Jul 29, 2017 · 6 comments
Labels
Bug Addressing a bug

Comments

@stiv-yakovenko
Copy link

This is my source code in nodejs:

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var abi = ...
var contract = new web3.eth.Contract(abi, MAIN_CONTRACT_ID);
contract.events.Evt({fromBlock: 0, toBlock: 'latest'},function () {
}).on('data',function (evt) {
        console.log("evt",evt);
    });

But I get this crash:

TypeError: Cannot read property 'on' of undefined
    at Subscription.subscribe (C:\Users\steve\Dropbox\Projects\dsd\dsd\web3test\node_modules\web3\packages\web3-core-subscriptions\src\subscription.js:199:45)
    at Contract._on (C:\Users\steve\Dropbox\Projects\dsd\dsd\web3test\node_modules\web3\packages\web3-eth-contract\src\index.js:623:18)

contract.Evt is undefined for me, contract is defined and contains contract.events.Evt.

How can I fix this?

@jdkanani
Copy link
Contributor

I cannot reproduce this.

@stiv-yakovenko
Copy link
Author

@jdkanani here is a full code:

https://gist.github.com/stiv-yakovenko/26994bbe32457bb69ef7694e98a5e7cf

Hopefully it can help you to reproduce this. I am on test-net.

@frozeman frozeman added the Bug Addressing a bug label Aug 3, 2017
@frozeman
Copy link
Contributor

frozeman commented Aug 3, 2017

The problem seems to be with setProvider, which doesn't propagate down the provider. i will fix it.
But in any case you can't use HTTP to subscribe, as it doesn't provide push and we don't have polling for subscriptions yet.
Please use Websockets.

And yes, event s are now under the event property

@jdkanani
Copy link
Contributor

jdkanani commented Aug 3, 2017

@frozeman Why not use direct methods/events - contract.transfer (like earlier) instead of contract.methods.transfer? Before it was making more sense. Any specific reason?

Apart from that, how one can get parsed log from raw log object from contract object? Example: contract.events.Transfer.parse(rawLog)? where rawLog is object with topics array etc..

@frozeman
Copy link
Contributor

frozeman commented Aug 3, 2017

@jdkanani the reason to put it under a separate property is that you can define any name on your contract object, which could interfere with native functions like deploy etc. So its safer to put it in a separate property. We also expose now the function names and signatures, so to make sure you can access all functions on a smart contract, even the ones with different parameters.

For contract raw logs you should have the raw property in the log itself.
I don't know about any parse function

@jdkanani
Copy link
Contributor

jdkanani commented Aug 3, 2017

@frozeman Makes sense. Thanks.

For log related, when you do getTransactionReceipt, you receipt with logs (having topics and data properties in it). I want to parse that log with appropriate event. So I can get appropriate returnValues. Receipt could be retrieved from anywhere, example - private server or using getTransactionReceipt method

Currently, I am using this (little hack):

var transferEvent = ct.events.Transfer();
var result =  transferEvent.options.subscription.outputFormatter(receipt.logs[0]); // receipt object from server or `getTransactionReceipt` get method
console.log(result.returnValues._to, result.returnValues._from, result.returnValues._value);

Is there any other way to do so? Like - ct.events.Transfer.outputFormatter(receipt.logs[0]) or other way without creating event object?

frozeman added a commit that referenced this issue Aug 3, 2017
@frozeman frozeman closed this as completed Aug 3, 2017
nachomazzara pushed a commit to nachomazzara/web3.js that referenced this issue Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Addressing a bug
Projects
None yet
Development

No branches or pull requests

3 participants