0

I am trying to find a way to diagnose whether my profile array is empty or not. I have seen that there are several posts dealing with this question, for example here . I apply the logic presented in this post, but the length of my profile array is always 1 eventhough it contains no data.

Basically my situation is that the profile is feteched from the backend, during this feteching period the profile is null. After the fetching was successful, the array contains a single object. So the length of the array should be 0 at the beginning and then after the fetching 1. However the length of the array is always 1 (see the console log).

export class Header extends Component {
    

    render() {

        const profile = this.props.profile;
        const isFetching = this.props.isFetching
        
        console.log (isFetching);
        console.log(Object.keys(profile).length);
        console.log (profile);


        return (<h1>Loading ...</h1>);
}
}

const loadingSelector = createLoadingSelector(['GET_USER', 'GET_PROFILE']);

function mapStateToProps(state, ownProps) {
    const profile = state.profile
    const isFetching = loadingSelector(state)

  return { profile, isFetching}
};

export default connect(
    mapStateToProps,
    { logout }
)(Header);

Console Log:

header.js:16 false //isFetching
header.js:17 1 // length of the profile Array
header.js:18 {profile: null}
header.js:16 true //isFetching
header.js:17 1 // length of the profile Array
header.js:18 {profile: null}
header.js:16 true //isFetching
header.js:17 1 // length of the profile Array
header.js:18 {profile: null}
header.js:16 false //isFetching
header.js:17 1 // length of the profile Array
header.js:18 {profile: Array(1)}

I wonder why the profile array has always the length 1 although it is null at the beginning?

dan_boy
  • 1,735
  • 4
  • 19
  • 50

1 Answers1

0

when console.log(profile) prints {profile: null} it means that profile is not empty and has a field of profile inside it so you always get 1 , you should change the way you pass data to your component or use profile. profile to get results you want. this what you are passing:

profile:{
   profile: null
}
ErfanFi79
  • 36
  • 6