0

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.

user7421798
  • 103
  • 11

2 Answers2

1

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.

rival
  • 76
  • 1
  • 3
  • Yes, This is working inside the instance but if i wanna console this outside of instance it's not showing data. Just showing an empty curly brace. Thanks a lot, dear. – user7421798 Oct 31 '17 at 04:27
0

By chaining the calls with .then(...), the returned value is of type Promise and not the Result object that you are expecting (and what's written to the console) and cannot be accessed by index. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then for more information.

rival
  • 76
  • 1
  • 3