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 Answers2

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.

sfmiller940
  • 498
  • 1
  • 3
  • 9
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

Ismael
  • 30,570
  • 21
  • 53
  • 96
rahul_eth
  • 556
  • 1
  • 4
  • 11
  • I am trying to observe events and get a callback when they occur –  Apr 28 '18 at 03: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 02 '18 at 07:15