0
console.log(TransactionDataPromise[0]);

logs the following object:

Promise {
  {
    blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
    blockNumber: null,
    from: '0x88B837d3528Fb13192e0b7b1009632A8372d8c8a',
    gas: 1000000,
    gasPrice: '15000000000',
    hash: '0xa1d5a80b738504acb30663c7d7ae869c52ca5430738c679536f8dfb72e770610',
    input: '0x741dfb1e000000000000000000000000000000000000000000000000000b8bdb9785200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d',
    nonce: 12530,
    to: '0x6f9B3f0640Bf7358C87d1d7f2Df1A546Df0E8C08',
    transactionIndex: 0,
    value: '0',
    v: '0x2b',
    r: '0xcd46cea5babf84761ac4aaf7d9351264f51f82c0739829ad1d9a336d03a6900d',
    s: '0x61314be797bb7fcbf00bfeb6e34d318281cac58f665bdc26e38cf7e718d0abf6'
  }
}

However,

console.log(TransactionDataPromise[0].to);

shows undefined, whereas I expected 0x6f9B3f0640Bf7358C87d1d7f2Df1A546Df0E8C08. How do I access this to property?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
igotBAWS
  • 105
  • 1
  • 11
  • 1
    the word `Promise` suggests asynchrony so, what you see in the console isn't necessarily the current "state" of that object, it's the state when you inspect it in the console – Jaromanda X Jun 28 '18 at 23:27
  • 2
    does `console.log(typeof TransactionDataPromise[0].then)` produce `function` - if so, then `TransactionDataPromise[0].then(o => console.log(o.to))` should log what you expect - and, see the link in the above comment – Jaromanda X Jun 28 '18 at 23:28
  • try `TransactionDataPromise[0].then((data) => console.log(data)) – Geuis Jun 28 '18 at 23:36
  • @Geuis Or `.then(({to}) => console.log(to))` directly. – Sebastian Simon Jun 28 '18 at 23:37
  • I have already "waited" for the data and have it, hence why i get the first output. fyi Other part of my code before i do the outputs: var allPromises2 = Promise.all([ TransactionDataPromise[0] ]); var sendPromise2 = allPromises2.then(function(results2){ – igotBAWS Jun 28 '18 at 23:48
  • @JaromandaX Indeed it produced `function`. However, the code you suggested didn't produce anything. :/ – igotBAWS Jun 29 '18 at 00:00
  • really? any errors in the console? what about `TransactionDataPromise[0].then(o => console.log(o))` – Jaromanda X Jun 29 '18 at 00:08
  • you seem to be faffing around with Promise.all, then another promise ... are you attempting to use so much indirection of the promise that you hope it will eventually turn synchronous? – Jaromanda X Jun 29 '18 at 00:10
  • @JaromandaX I was mistaken. The first solution indeed worked, it just had produced the expected result A LOT later. I struggle to understand why I can't directly access the Promised object's data which I obviously already have, proof `console.log(TransactionDataPromise[0]);` I would be more happy, however I will have to check all the `array TransactionDataPromise`. I guess you can submit your comment as a solution! – igotBAWS Jun 29 '18 at 00:17
  • I use two Promises, because only after I get the first one (Ethereum Block) I am able to find the "to" Address over the asynch API I am using. – igotBAWS Jun 29 '18 at 00:19
  • I found the solution. It is to get the result by `console.log(results2[0].to);` instead of calling the `console.log(TransactionDataPromise[0]);` – igotBAWS Jun 29 '18 at 00:25

0 Answers0