1

I'm having only partial success with a Node.js script I wrote that interacts with one of my Ethereum Smart Contracts.

Here's what's working:
-I'm able to call methods in the Smart Contract from my Node.js script
-I'm able to capture and display the results returned from these function calls using my Node.js script

The problem I'm having has to do with capturing EVENTS that are emitted by the contract.

My code won't run cause I keep the following error:

TypeError: theContract.saleTXReceivedEvent is not a function

What's weird is the code I'm using in my Node.js file is the exact same code I've been using in my regular client-side JS files (which are embedded into my HTML files) - which work perfectly well.

Here's the code:

var capturedEvent = theContract.saleTXReceivedEvent();
capturedEvent.watch(function(error, result) {
  if(!error) {
    console.log("Sale was successful!");
    console.log("Results are as follows: ", result);
  }
  else {
    console.log("ERROR!!!! Details: ", error);
  }
});

So again, this code works perfectly in my client-side web files, but it gives me an error when in my node.js file:

TypeError: theContract.saleTXReceivedEvent is not a function

It is of course true that saleTXReceivedEvent is not a Function - its an EVENT, but in terms of the syntax, how else am I supposed to reference it? Why is it working fine in my regular JS files but failing in my NodeJS files?

What's going on here?

  • which version of web3 you are using? – Mahesh Rajput Jan 21 at 5:15
  • I'm on version 1.2.4 – Sirab33 Jan 21 at 6:17
0

theContract.events.yourEventName is neccesary.

const theContract = new web3.eth.Contract(abi, contractAddress, {
    from: fromAddress,
    gasPrice: 200000000
});
theContract.events.saleTXReceivedEvent((error, event) => {
    console.log(event);
});
| improve this answer | |
  • 1
    ok, that worked - but where is the watch portion of the code now? I originally assigned the Event to a variable called capturedEvent, and in the next statement did capturedEvent.watch(function(error, result) { } etc. - but your code doesn't have watch at all. How will that work then? – Sirab33 Jan 21 at 6:16
  • I'm sorry, I don't understand probably older version syntax .watch. Bellow @goodvibration's answer would help you. – 久保圭司 Jan 22 at 3:45
  • Yep, I got it. .watch is used in web3.js V. 0.x.x, while in V. 1.x.x it is no longer needed. – Sirab33 Jan 22 at 5:15
0

What's weird is the code I'm using in my Node.js file is the exact same code I've been using in my regular client-side JS files (which are embedded into my HTML files) - which work perfectly well.

You clearly have two different versions of Web3.js installed:

  • Your client-side environment relies on Web3.js v0.x.
  • Your NodeJS environment probably relies on Web3.js v1.x.

Under Web3.js v0.x, you should use theContract.saleTXReceivedEvent(); see this example.

Under Web3.js v1.x, you should use theContract.events.saleTXReceivedEvent(); see this example.

| improve this answer | |
  • Hey @goodvibration you're right about how I'm using 2 different versions of web3 - and about needing to add .events to my code for it to work. I'd mark yours as the correct answer but @engineerk provided that answer first. Really appreciate your contribution to this question though - I'm sure it'll be helfpul to future readers. – Sirab33 Jan 22 at 2:33
  • @Sirab33: You're welcome :) – goodvibration Jan 22 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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