Storing a JSON response from the IGDB API into an array: A guide to seamless data organization

I'm currently learning JavaScript and I've started working on a mobile application using React Native. However, I've encountered some issues with the IGDB API and the "Fetch" method from MDN.

My problem lies in trying to store a JSON response into an array and display it using a FlatList. Unfortunately, nothing gets printed - not even the "Test:" text or the "{item.id}". I'm unsure if this is due to the API itself or my implementation of the FlatList component.

Interestingly, testing the request on Postman yields successful results.

When I make the request to the API, the response is correctly displayed in the terminal.

Below are the main JS files I'm using along with a warning message that pops up when I receive a response from the API.

Feel free to ask any questions you might have, and I'll do my best to provide clear answers.

//Search.js

import React from 'react'
import { View, TouchableOpacity, TextInput, StyleSheet, Image, FlatList, Text } from 'react-native'
import { getGamesFromApiWithSearchedText } from '../API/IGDB'

class Search extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            games: []
        }
    }

    render() {
        return (
            <View>
                <View>
                    <TextInput placeholder='Search games and stuff...' />
                    <TouchableOpacity onPress={() => this._loadGames()}>
                        <Image source={require('../images/icons/ic_search.png')}/>
                    </TouchableOpacity>
                </View>

                <FlatList
                    data={this.state.games}
                    extraData={this.state.games}
                    keyExtractor={(item) => item.id.toString()}
                    renderItem={({ item }) => <Text> Test: {item.id}  </Text>}
                />
            </View>
        )
    }

    _loadGames() {
        console.log("Search button game has been clicked")
        getGamesFromApiWithSearchedText().then(data => {
            this.setState({ games: data.results })
        })
    }
}

export default Search
//IGDB.js

export function getGamesFromApiWithSearchedText() {
    const url = 'https://api-v3.igdb.com/games'
    return fetch(url, {
        method: 'POST',
        headers: {
            "user-key": API_TOKEN
        },
        body:
            'fields id, name, release_dates, rating, game_engines, summary, involved_companies, genres; search "Witcher"; limit 1;'
    })
        .then(response => response.json())
        .then(data => data)
        .catch((error) => console.log(error))
}
//Warning I receive

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'data.results')]
- node_modules\promise\setimmediate\core.js:37:14 in tryCallOne
- node_modules\promise\setimmediate\core.js:123:25 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194:17 in _callImmediatesPass
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458:30 in callImmediates
* [native code]:null in callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:407:6 in __callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:142:17 in __guard$argument_0
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue

Answer №1

Hooray! I have successfully resolved the issue that was plaguing me. It turns out that the culprit causing the problem was the ".results" within the function "_loadGames()". After removing it, everything appears to be functioning smoothly now.

A huge thank you to everyone who lent their assistance!

Answer №2

In order to retrieve the data from the second .then, you must follow these steps:

export function getGamesFromApiWithSearchedText() {
    const url = 'https://api-v3.igdb.com/games'
    return fetch(url, {
        method: 'POST',
        headers: {
            "user-key": API_TOKEN
        },
        body:
            'fields id, name, release_dates, rating, game_engines, summary, involved_companies, genres; search "Witcher"; limit 1;'
    })
        .then(response => response.json())
        .then(data => data)
        .catch((error) => console.log(error))
}

To trigger a re-render of the flatlist when the state changes, make use of the extraData prop. This prop is essential for updating certain components based on changing data, such as a loading flag like isLoading.

<FlatList
   data={this.state.games}
   extraData={this.state.games}
   keyExtractor={(item) => item.id.toString()}
   renderItem={({ item }) => <Text> Test: {item.id}  </Text>}
/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Position the vertical bar directly adjacent to the form input field

Looking for assistance with creating a unique webpage layout that includes a form where the employee ID is visually separated from the rest of the content by a vertical bar extending across the entire page. However, my attempts to achieve this using a gr ...

Visuals and PDF Generation Tool

Trying to generate project report pdf's using pdfmake has presented a challenge when it comes to displaying images. A function I have for creating a pdfmake "object" looks like this: function singleProject(data) { return { text: "Project ...

What is the reason behind only the initial click boosting the vote count while subsequent clicks do not have the same

In this snippet of code: //JS part echo "<script> function increasevotes(e,location,user,date,vote) { e.preventDefault(); var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState ...

Syntax error: string unexpected, caught off guard

While attempting to create a drop-down menu, it functions properly in Firefox but fails to work in other web browsers. The error message that appears is as follows: jQuery(".parent").hover( function () { jQuery(this).toggleClass("activeli").(".paren ...

Choosing a portion of a polyline - Google Maps Application Programming Interface

Is it possible to select only a section of a polyline in a Google Map? I have markers that are added by the user and a polyline is drawn between the markers. The polyline between two markers represents the route between the two places. I want the user to b ...

Connecting to a pathway of Material-UI menu items

I am currently utilizing Material-UI's menu components as part of my project requirements. However, I am encountering difficulties in properly routing each MenuItem to an existing route within my application. As a temporary solution, I have resorted ...

JavaScript: Remove duplicate values from one array by comparing and utilizing a search filter against another array

There are two arrays available: public favoriteTeams: any[] = [ { name: 'Team Batman' }, { name: 'Team Superman' }, { name: 'Team Darkseid' }, { name: 'Team Wonder Woman' } ]; public searchTeams: any[] = [ ...

Embarking on the Mongoose Journey

Every time I enter the code const mongoose = require("mongoose") An error message is displayed: /Users/user/shares3/node_modules/mongodb/lib/utils.js:1069 catch { ^ SyntaxError: Unexpected token { at createScript (vm. ...

Is it possible for a Jquery radio button to trigger an infinite loop?

When I click on a radio button, I want to receive an answer from the PHP file. However, when I use the radio button, the alert appears in an endless loop. Why does this happen and how can I make the alert display only once? I tried with just a regular but ...

Why was the event handler attached and for what purpose does it serve in this particular location?

When using Typescript with JQuery, I encountered a strange issue where a click event seemed to be added multiple times each time the user opened a dialog. Despite creating a new SettingsDlog object for each dialog instance, the click event did not behave a ...

Tips for transforming a style object into a compiled attribute value using Vue API

Can the Vue 2.x API be used to convert an object representing component styles into a format suitable for the style DOM attribute? Essentially, I am seeking an imperative API similar to the v-bind:style directive. For instance: const style = { fontSiz ...

Switch content based on value with Javascript

One of my elements is: <a href="#" type="link" class="button send" classAct="button send" classSel="button send pressed" label="encrypt" title="sendmessage" onclick="add_encryption();">Encrypt</a> When toggled via the JavaScript below, I want ...

Enhancing Next.js Images with Custom CSS Styles

I am working with a Next.js component: import styles from '../styles/Navbar.module.css' import Image from 'next/image' export const Navbar = () => { <div> <Image className={styles["navbar-home-icon"]} ...

Having trouble setting the function props type for its child in Typescript? Getting an error that says "Property 'children' does not exist on type TS2339"?

To provide context, my development environment includes React version 16.9.0 and Typescript version 3.5.3. To replicate the issue, you can install Create React App and create a Typescript project, then add this minimal example: import React from 'rea ...

Tips for accessing array values dynamically in Vuejs from an existing array?

WelcomeWorld.vue export const dataList = [ { id: 1, val: "11", kk: "potter" }, { id: 2, val: "22", kk: "james" }, { id: 3, val: "55", kk: "limda" }, { id: 4, val: "77", kk: "stepen" } ]; <template> <div> <b>Vuejs dyn ...

Get the object method within an AJAX success callback

Is there a way for me to access the FileUploader.prototype.saveImage() method in my code? Here is an example snippet: function FileUploader(object) { this.checkInputs(object); if (this.isImageSelected()) { this.beforeInit(object); ...

Using Foreach to reference data as "this"

I am attempting to iterate through this information in order to assign a value to each. const keys = Object.keys(response.data) keys.forEach((index, element) => { // let query = "this."+element this[element] = response.data[element] }); The de ...

Synchronization issues with form validation

I'm currently tackling an assignment for my CS50 course and I am relatively new to working with Jquery/Ajax. As part of this task, I am developing a registration form where users must enter a unique username. To achieve this, I have implemented code t ...

What are the best methods for concealing the URL or video source in JWPlayer 7 or Flowplayer version 6.0.5?

Is there a way to conceal the URL of a video from being viewed in the browser's inspect element? I'm looking for a method to encrypt it and prevent IDM from downloading the video. flowplayer("#fp-hlsjs", { key: "$**********", logo: "<?= Y ...

Unable to update the $_SESSION variable while two synchronous ajax calls are being processed simultaneously

Here's a tricky situation I've run into. I'm currently working on a form that verifies certain elements upon submission, using event.preventDefault(); to halt the submission process if any issues arise. The problem arises when multiple ajax ...