简体   繁体   中英

Node JS Interacting with Ethereum Smart Contract

I'm having 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?

I think you forgot events - try this

var capturedEvent = theContract.events.saleTXReceivedEvent();
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM