Search code examples
ethereumsoliditycontractweb3jsgo-ethereum

Store contract results tag in a variable


I have a web3 contstant look like this.

const test = contract_instance.methods.getPost(15).call().then(console.log);

This returns results like this.

Result {
  '0': '2017-08-28',
  '1': '19:18:04.986593',
  '2': '07:17:00',
  '3': '11112323',
  '4': '12',
  date: '2017-08-28',
  login_time: '19:18:04.986593',
  logout_time: '07:17:00',
  login_device_id: '11112323',
  user_id: '12' }

Now when i want to console single tag through console.log(test[0]); this returns undefined My approach is to store every result tag in its individual variable. Need some suggestion.


Solution

  • then() takes a function as argument that is called on fulfillment of the promise. You can then add the value to an array for example:

    var results = []
    contract_instance.methods.getPost(15).call().then(function(value){
       console.log(value)
       results.push(value)
    });
    

    results[0] would then be the result object you are looking for and results[0]['0'] would give you the date '2017-08-28' for example.