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!

15
  • 2
    Can you replicate this in a snippet here? It would be nice to see more of the code Commented Oct 15, 2020 at 10:48
  • 2
    Please provide a minimal reproducible example Commented Oct 15, 2020 at 10:49
  • 1
    Possibly related: Is Chrome's JavaScript console lazy about evaluating arrays? Commented Oct 15, 2020 at 10:53
  • @TasosBu - not easy to reproduce as the code's spread out amongst redux store stuff... Commented Oct 15, 2020 at 10:54
  • @Jamiec - unfortunately, I can't do that very easily... Commented Oct 15, 2020 at 10:55

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

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).
Can you verify that .length works as expected by running console.log([1].length) ?
Also try and print data and data.length in the same console.log like this: console.log(data, "length: ", data.length)
@TasosBu - I do that (console.log(data, data.length) - I just simplified for the example. It produces this: Array(4) [ {…}, {…}, {…}, {…} ] 0
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...

1 Comment

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 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.