1

console.log(data) reveals the foll​owing:

(4) […]
​
0: "a19870e84cc92abeb5904fccd1f74329"
​
1: "12793d4229b32a0b1fc387e041ed7be8"
​
2: "12793d4229b32a0b1fc387e041ed7be8"
​
3: "5fec6cd9e7c78a4a985d1f5b82f05280"
​
length: 4

Yet, console.log(data.length) says the following:

0

..and indeed, I'm unable to loop through the array with a for loop because data.length = 0.

How can my array have 4 elements but have zero length all at once? I'm confused.

ps. the array gets produced via an async' redux function, here:

export const getFiles = () => {
  return async (dispatch: AppDispatch, getState: Function) => {

    const state = getState()
    const scriptAddress = state.chainInfo.data.scriptAddress

    var fileData: string[] = []

    api.cmd("coins;", function(respJSON: any) {

      if( api.util.checkAllResponses(respJSON) ) {

        //console.log(respJSON)
        const coins = respJSON[0].response.coins
        for ( let i = 0; i < coins.length; i++ ) {
          if (coins[i].data.coin.address == scriptAddress) {
            fileData.push(coins[i].data.prevstate[0].data)
          }
        }
      }
    })
  
    dispatch(write({data: fileData})(GetActionTypes.GET_SUCCESS))
  }
}

And it updates a react component via mapStateToProps - so it only gets the data after the async operation has updated the store via a reducer. So I don't 'think' it's an async problem.

And gosh - I should've mentioned all the react, redux, mapState stuff. Sorry!

glowkeeper
  • 209
  • 2
  • 11

2 Answers2

1

length is just an property, you are able to overwrite by mistake for example:

const a = [1, 2, 3, 4];
console.log('before: ', a.length)
a.length = 0;
console.log('after: ', a.length)
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
  • Yes - and I'm accessing that property above - you'll see it says '4' when I log the data, but '0' when I log data.length. I promise you I am not overwriting it in between calls (at least, not intentionally). – glowkeeper Oct 15 '20 at 10:53
  • Can you verify that .length works as expected by running `console.log([1].length)` ? – Tasos Oct 15 '20 at 10:54
  • Also try and print data and data.length in the same console.log like this: `console.log(data, "length: ", data.length)` – Tasos Oct 15 '20 at 10:57
  • @TasosBu - I do that (`console.log(data, data.length)` - I just simplified for the example. It produces this: Array(4) [ {…}, {…}, {…}, {…} ] 0 – glowkeeper Oct 15 '20 at 11:14
0

FIXED!

I moved the dispatch inside api.cmd, and data.length behaves correctly:

export const getFiles = () => {
  return async (dispatch: AppDispatch, getState: Function) => {

    const state = getState()
    const scriptAddress = state.chainInfo.data.scriptAddress

    var fileData: string[] = []

    api.cmd("coins;", function(respJSON: any) {

      if( api.util.checkAllResponses(respJSON) ) {

        //console.log(respJSON)
        const coins = respJSON[0].response.coins
        for ( let i = 0; i < coins.length; i++ ) {
          if (coins[i].data.coin.address == scriptAddress) {
            fileData.push(coins[i].data.prevstate[0].data)
          }
        }
      }
  
      dispatch(write({data: fileData})(GetActionTypes.GET_SUCCESS))
    })
  }
}

Boom! Thanks for all the comments...

glowkeeper
  • 209
  • 2
  • 11
  • 1
    Glad you solved your problem - we never really had a chance of helping you with the information you originally provided. Next time include the code causing the problem and we'll be more successful :) – Jamiec Oct 15 '20 at 11:52