1

I'm trying to fetch data from the url below, but when I launch the code it returns the error: TypeError: undefined is not an object (evaluating 'res.data.hints'), and consequentially nothing happens, I've followed various tutorials and they seem to come up with this code.

States

constructor(props) {
    super(props);
    this.updateSearch();
    this.state = {
      data: [],
    };
  }

Function

 updateSearch = () => {
        const url = `https://api.edamam.com/api/food-database/v2/parser?ingr=b&app_id=000&app_key=00000&page=20`;
        fetch(url)
           .then((res) => res.json())
           .then((res) => {
             this.setState({
               data: res.data.hints
             });
           })
           .catch(error => {
            console.log("error : "+ error)
          });
      };

Flatlist

     <FlatList
          data={this.state.data}
          renderItem={({ item }) => (
            <ListItem>
              <TouchableOpacity>
                <View>
                  <Text>{item.label}</Text>
                  <Text>{item.brand}</Text>
                </View>
              </TouchableOpacity>
            </ListItem>
          )}
          keyExtractor={(item) => item.foodId}
        />

1 Answer 1

3
this.setState({
           data: res.hints
         });

Try this

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

5 Comments

It now shows only the lines of the separator of the Flatlist, no labels appear in the Flatlist and also it returns the Warning: ` Each child in a list should have a unique "key" prop.`
To remove the warning you will have to provide the a key props to the parent component where you are mapping the fetched data
I'm generally new to programming, how can I do that?
<ListItem key={item.food.foodId}>, and also to render the items on screen you will have to use the following code : <FlatList data={this.state.data} renderItem={({ item }) => ( <ListItem key={item.food.foodId}> <TouchableOpacity> <View> <Text>{item.food.label}</Text> <Text>{item.food.brand}</Text> </View> </TouchableOpacity> </ListItem> )} keyExtractor={(item) => item.food.foodId} />
Hi welcome to Stack Overflow. Please consider giving a reason why this works.

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.