0

I am trying to make an API call, which works and this is shown by my console.log as this prints out what I am looking for. However, when I try to return the same value it doesn't return what I want / what was expected:

Function:

export async function TeamSquad({URL, TeamID}) {
    try {
        // StringCheck - Checks TeamID is a string
        StringCheck(TeamID);

        const apiResponse = await axios(paramOptions(URL, {
            team: TeamID,
        }));
        const response = apiResponse.data.response;

        // TeamSquadBasic - Fills an object with objects
        const Squad = TeamSquadBasicInfo(response);

        console.log(Squad["0"])  // This works as expected 

        // Count / Check position count
        // PlayersInPosition(Squad);
        return Squad["0"];   // This doesn't work as expected

    } catch (error) {
        console.error(error);
    }
}

How I call the function (For example purposes):

test("Squad Line Up", async () => {
    const Lineup = await TeamSquad({
        URL: 'https://api-football-v1.p.rapidapi.com/v3/squads', 
        TeamID: '33'
    });

    expect(Lineup).toEqual(LineupHeaton);  // LineupHeaton is Squad["0"] hardcoded
});

The result of Squad["0"]:

{ "Plays For": "Manchester United", "name": "T. Heaton", "age": 37, "number": 22, "position": "Goalkeeper", "photo": "https://media-3.api-sports.io/football/players/2931.png" }

When I console.log this the result comes out like ^, but when I try to return this it comes out like:

{"_h": 0, "_i": 0, "_j": null, "_k": null}

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
TedSnow
  • 11
  • 2
  • 2
    `async` functions return a Promise ... `await` it `const test = await TeamSquad(....)` or use `.then` like `TeamSquad(....).then(test => console.log.....` – Jaromanda X Aug 15 '23 at 22:36
  • @JaromandaX I used that as a temporarily example, I am really trying to do use it in a `test()` but keep getting a undefined result. With my Test function looking like: `test("Squad Line Up", async () => { const Lineup = await TeamSquad({ URL: 'https://api-football-v1.p.rapidapi.com/v3/squads', TeamID: '33' }); expect(Lineup).toEqual(LineupHeaton); });` LineupHeat is just the Squad["0"] result hardcoded – TedSnow Aug 15 '23 at 22:40
  • edited the question, to reflect ^ – TedSnow Aug 15 '23 at 22:44
  • Does this answer your question? [Can't access object property, even though it shows up in a console log](https://stackoverflow.com/questions/17546953/cant-access-object-property-even-though-it-shows-up-in-a-console-log) – Heretic Monkey Aug 15 '23 at 22:46
  • so, in your modified code, does `Lineup` look like the **object** you expect? I don't know about whatever test suite you use, but in Javascript `{} !== {}` ... so, perhaps that's now your new problem - i.e. you're trying to compare **Objects** – Jaromanda X Aug 15 '23 at 22:50
  • @JaromandaX, I am trying to compare to ***objects***. However, the test result is saying `Received: undefined` indicated that `Lineup` isn't receiving what I expect it to receive. When I `console.log(Squad["0"])` the result is as expected but returning the same thing is coming back as undefined – TedSnow Aug 15 '23 at 22:57
  • @HereticMonkey, I don't believe that is what I am looking for – TedSnow Aug 15 '23 at 23:00
  • if `Lineup` is undefined in your new code, then there's something else you're not showing. Are you sure there's no error in the request? that would result in `undefined` since that's what you return in the case of an error – Jaromanda X Aug 15 '23 at 23:21
  • After returning `TeamSquadBasicInfo(response)` to `Squad`. I can `console.log(Json.stringify(Squad))` perfectly fine, same for `console.log(Json.stringify(Squad[0]))`. The results come out as expected. But once I get to returning the value, it comes back as undefined – TedSnow Aug 15 '23 at 23:26

1 Answers1

1

Your function TeamSquad is async, which means it returns a Promise. You have to resolve this promise to get data within. There are two ways of doing this:

TeamSquad({
  URL: 'https://api-football-v1.p.rapidapi.com/v3/players/squads',
  TeamID: '33'
}).then((test) => {
  console.log(JSON.stringify(test, null, 2));
})

// Keep in mind, the result is not available in this scope
// It is only available within the scope of the callback function in your `.then()`

or, using the await keyword. Keep in mind this can only be used within other async functions:

const test = await TeamSquad({
  URL: 'https://api-football-v1.p.rapidapi.com/v3/players/squads',
  TeamID: '33'
})

console.log(JSON.stringify(test, null, 2));

You can read more about async/await and Promises here.

spikehd
  • 234
  • 1
  • 6
  • I updated the code to reflect where I am actually trying to use the `TeamSquad` function, even with the `await` keyword it is still returning undefined value back to `Lineup` – TedSnow Aug 15 '23 at 22:46