3

I am trying to access an event from my contract to observe in Javascript. I expect to get c.QuantumPilotKeyPurchased but that is undefined

var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
let c = new web3.eth.Contract(abi, keyaddress);
console.log(c.events);
console.log(c.QuantumPilotKeyPurchased);

Output:

node server.js
{ QuantumPilotKeyPurchased: [Function: bound ],
  '0x55985f5332be13e699aaa51a5d3c003941f60e74af84533ebab5626b72cf3f51': [Function: bound ],
  'QuantumPilotKeyPurchased(address)': [Function: bound ],
  allEvents: [Function: bound ] }
undefined

See the above^

Why is this happening?

2 Answers 2

1

AFAIK an event function is used within a contract to create events in the blockchain. But you can't call an event function directly from outside of the contract (with web3 for example). Instead you could probably make a public function in your contract to create the event.

If you want to see already created events, you can "get" past events or "watch" for new events on the blockchain with web3. See this post for more information.

2
  • "myContract.myEvent().watch" - this is what I'm trying with c.QuantumPilotKeyPurchased() -- it's undefined
    – user22075
    Apr 28, 2018 at 3:37
  • Have you tried "console.log( c.events.QuantumPilotKeyPurchased );"? Apr 28, 2018 at 4:14
1

The correct syntax is

var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

let c = new web3.eth.Contract(abi, keyaddress);
c.events.QuantumPilotKeyPurchased((err, transaction) => {
   console.log(err, transaction);
})

Hope it will help you

2
  • I am trying to observe events and get a callback when they occur
    – user22075
    Apr 28, 2018 at 3:37
  • myContract.myEvent().watch((error, result) => { if (error) console.log('Error in myEvent event handler: ' + error); else console.log('myEvent: ' + JSON.stringify(result.args)); });
    – rahul_eth
    May 2, 2018 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.