React Native : json.data is not a function. (In 'json.data()', 'json.data' is an instance of Array)]

Viewed 84

I'm trying to display data from my web using API. My API is working correctly. But some how i face this issue json.data is not a function. (In 'json.data()', 'json.data' is an instance of Array)]. Can I anyone help me on this?

 _loadInitialState = async () => {

        const token = await AsyncStorage.getItem('token');
        const myArray = await AsyncStorage.getItem('language_type');
        _onSetLanguage(myArray)

        var loadAll = this.props.navigation.getParam('loadAll');
        if (loadAll == true) { this.setState({ isLoading: true }) }

        var have_access = this.props.navigation.getParam('have_access');
        if (have_access != undefined) { this.setState({ have_access: have_access }) }
        if (staticData.get_centre_settings() != null) { this.setState({ have_access: true }) }

        let today = Moment();
        this.setState({
            today: Moment(today).format('YYYY-MM-DD'),
        })

        // alert(FetchURL.get_baseURL() +  "leave?centre_id=" + staticData.get_centre_settings().centre.centre_id)
        // alert(token)
        // return
        fetch(FetchURL.get_baseURL() +  "leave?centre_id=" + staticData.get_centre_settings().centre.centre_id, {
            method: 'GET',
            headers: {
                'Authorization': 'Bearer '+ token,

            }
        })
        .then(response => response.json())
        .then((json) => {
            // alert(json)
            if (json.error) {
                alert(json.error);
            } else {
                this.setState({
                    staffLeave: json.data(),
                    isLoading: false
                });


            }

        })
        .catch((err) => {
            console.log(err)
            alert(err)
            // alert(strings.data_fail);
        })
    this.setState({ view_access: true })


    }

I kept searching for this but couldn't find an answer that will make this clear.

Thanks!

3 Answers

You have the response after response.json() as json. If you want to store it to state, use json.data and not json.data(), just as you check for json.error.

When you read the error json.data is not a function. (In 'json.data()', 'json.data' is an instance of Array)]

What this is telling you is that json.data is not a callable function. Therefore json.data() will not work. json.data is an array per the error message. You would access some part of that by: json.data[0] But you'd want to check the length of that array and understand the type (which Typescript helps solve).

// alert(json)
            if (json.error) {
                alert(json.error);
            } else {
                this.setState({
                    staffLeave: json.data(),
                    isLoading: false
                });


            }

Replace with

 if (json.error) {
                alert(json.error);
            } else {
                this.setState({
                    staffLeave: json.data,
                    isLoading: false
                });
            }

hopefully, it's work.

Related