1

I successfully posted data in MongoDB Atlas, Now i want to display that data in my simple react native App. Data is showing in my terminal but i am not able to display data in my App.

Here is code for Get data form the db.

display(){
  fetch('myUrl', {
  method: 'GET'
})
 .then((response) => response.json())
 .then((responseJson) => {
   console.log(responseJson);
   this.setState({
     title: responseJson,
     description: responseJson
   })
 })
 .catch((error) => {
   console.error(error);
 });
}

Here is the code that is not displaying the data in App

<TouchableOpacity onPress={()=>this.display()} style={styles.btn}>
  <Text style={{textAlign: 'center'}}> Display </Text>
</TouchableOpacity>

<View>
  <FlatList
    data={this.state.title}
    renderItem={({item}) => <Text>{item.title}</Text>}
    keyExtractor={({id}, index) => id}
    />
</View> 
6
  • can you also share what come under responseJson Commented Dec 9, 2019 at 7:47
  • In responceJson I got all the posted data like title, description etc on my terminal. But when I add flat list for it is giving the error null is not an object (this.state.title) Commented Dec 9, 2019 at 7:53
  • can you alos share your console where you print this line console.log(responseJson); Commented Dec 9, 2019 at 7:54
  • Object { "__v": 0, "_id": "5dede264e74a14d7e284", "date": "2019-12-09T05:48:50.721Z", "description": "Bad", "title": "Monday", }, ] Commented Dec 9, 2019 at 7:59
  • I am getting the data but i am not able to display on screen properly that is the issue Commented Dec 9, 2019 at 8:00

3 Answers 3

1

Flatlist data property expects an array.

But you seem to set an object.

If your api returns an array you can make the following changes to make it work:

state = {
   items:[]
}

display() {
  fetch('myUrl', { method: 'GET'})
 .then((response) => response.json())
 .then((responseJson) => {
   console.log(responseJson);
   this.setState({
        items: responseJson
   })
 })
 .catch((error) => {
   console.error(error);
 });
}

As you see I used items in state as array, and updated its value when I got response from api.

And in flatlist:

<View>
  <FlatList
    data={this.state.items}
    renderItem={({item}) => <Text key={item._id}>{item.title}</Text>}
    keyExtractor={ item => item._id}
    />
</View> 

A sample codesandbox

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

7 Comments

i got an error with this code > TypeError: null is not an object (evaluating 'this.state.items')
@AbdulWahab did you add this code to your component state = { items:[] }
@AbdulWahab does you api returns an array of objects?
Yes, this object returning two item > title and description
@AbdulWahab is it like [{"title":"t1", description:"d1}, {"title":"t2", description:"d2} ] ?
|
1

Update your code like this:

this.state = {
  responseData:[]
}
display = () => {
  fetch('myUrl', {
  method: 'GET'
})
 .then((response) => response.json())
 .then((responseJson) => {
   console.log(responseJson);
   this.setState({
     responseData: responseJson,
   })
 })
 .catch((error) => {
   console.error(error);
 });
}

inside your render function:

render(){
  const { responseData } = this.state;
  return(
    <TouchableOpacity onPress={()=>this.display} style={styles.btn}>
      <Text style={{textAlign: 'center'}}> Display </Text>
    </TouchableOpacity>

    <View>
      <FlatList
        data={responseData}
        renderItem={this.renderItem}
        keyExtractor={item => item.id}
        />
    </View> 
  );
}
renderItem = ({item}) => {
  const { title, id, description, date } = item;

  <View key={item.id}>
    <Text>{item.id}</Text>
    <Text>{item.title}</Text>
    <Text>{item.description}</Text>
    <Text>{item.date}</Text>
  </View>
}

4 Comments

Hope it will help you @Abdul Wahab
Tysm for your help. BTW It is throwing an err on this ** const { responseData } = this.state;** and the error is > TypeError: null is not an object (evaluating 'this.state.responseData')
have you define` this.state = { responseData:[] }` or Can you share your code ?
Please share your code , it will helpful for anyone to understand your issue.
0

try with return keywork
Working demo api

display(){

     return fetch('myUrl', {
      method: 'GET'
    })
     .then((response) => response.json())
     .then((responseJson) => {
       console.log(responseJson);
       this.setState({
         title: responseJson,
         description: responseJson
       })
     })
     .catch((error) => {
       console.error(error);
     });
    }

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.