0

I am new in react-native.I am unable to get "present" data from my API and I am getting an error "cannot read property present" so i just want to get my API response.Here is my api data and please check my code too.

{
 "message": "present in this month",
  "data": [
    {
        "present": 10
    }
   ],
  "status": "1"
 }


  async componentDidMount() { 
 try {
            const DEMO_TOKEN = await AsyncStorage.getItem('isLoggedIn');
            if (DEMO_TOKEN != null) {
                console.log('Token', DEMO_TOKEN);
                fetch('http://104.197.28.169:3000/userPresentInThisMonth', {
                    method: 'GET',
                    headers: {
                        Authorization: 'Bearer ' + DEMO_TOKEN,
                    },                     
                })
                    .then(response => response.json())                     
                    .then(responseJson => {
                          alert(responseJson)              
                        console.log(responseJson);
                        this.setState({
                            presentmonthdata : responseJson,
                        });
                    });
            }
        } catch (error) {
            alert(JSON.stringify(error));
            console.error(error);
        }
   }

This is my Text value.

   {this.state.presentmonthdata.data[0] &&
    <Text   style={{color: 'grey',fontSize: 30,   fontWeight: 'bold',  }}>
                            {this.state.presentmonthdata.data[0].present}
                  </Text>

}

6
  • try this.state.presentmonthdata.data[0].present Commented Apr 27, 2020 at 14:20
  • I am getting an error "cannot read property '0' of undefined " . Commented Apr 28, 2020 at 4:57
  • since you are fetch data from API you should wait until data load. this.state.presentmonthdata.data[0] && this.state.presentmonthdata.data[0].present Commented Apr 28, 2020 at 6:11
  • i got an same error Commented Apr 28, 2020 at 6:57
  • I did update my code but i got an same error please check Commented Apr 28, 2020 at 7:01

3 Answers 3

1

Try this:

change:

{this.state.presentmonthdata.data[0] &&
    <Text style={{color: 'grey',fontSize: 30,   fontWeight: 'bold',  }}>
                            {this.state.presentmonthdata.data[0].present}
                  </Text>

to

<Text>
{this.state.presentmonthdata.data!= undefined ? this.state.presentmonthdata.data[0].present: null}
</Text>

Hope this helps!

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

Comments

0

the solution is

{this.state.presentmonthdata.data[0]}

6 Comments

I am getting an error "cannot read property '0' of undefined "
{ "message": "present in this month", "data": [ { "present": 10 } ], "status": "1" }
the problem is you didn't parse JSON In the response and you setState a JSON
please use response.json() before return response
your value and key is wrapped with " " and it is the problem
|
0

Try the following: {this.state.data.length > 0 ? this.state.data[0].present : 0;}

I'm guessing data is either a blank array or another data type...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.