Json Object returning 'undefined'

I have an api call to get NFL spreads. Here is the json object:


[{"id": "d5b75a02ec389dcad39c351b640054a6",
"sport_key": "americanfootball_nfl",
"sport_title": "NFL",
"commence_time": "2021-09-19T17:00:00Z",
"home_team": "Miami Dolphins",
"away_team": "Buffalo Bills",
"bookmakers": [{"key": "fanduel",
    "title": "FanDuel",
    "last_update": "2021-09-19T01:42:23Z",
    "markets": [{"key": "spreads",
    "outcomes": [{"name": "Buffalo Bills",
        "price": -106,
        "point": -3.5},
    {"name": "Miami Dolphins",
        "price": -114,
        "point": 3.5}]}]},

So I’m able to access everything using

getSpreads()  
  .then(
      data =>  {
        $w("#home").text = data.home_team;
   });

This is working fine, it returns ‘Miami Dolphins’, but if I try to return the spread like this:

 getSpreads()  
  .then(
      data =>  {
        $w("#home").text = data.home_team;
      $w("#homeSpread").text = "" + data.bookmakers[0].markets[0].outcomes[1].point;
      });

It returns undefined. I can access the name with data.bookmakers[0].markets[0].outcomes[1].name (this returns ‘Miami Dolphins’) so I know I’m on the correct branch of the tree, but if I try and return the number value for the spread (-3.5) it returns ‘undefined’.

This API returns a Json object with all strings, except for the spread which isn’t in quotes, (“point”: -3.5) so I assume it’s a number. I’m guessing this is my issue?

Gary, if you console.log

data.bookmakers[0].markets[0].outcomes[1].point

do you get 3.5 as a result? Or undefined? In any case, you are right that 3.5 is retained as a data type number in JSON. You cannot put that directly into a Wix text element, it accepts only strings. So you would do a .tostring() to make it right. But the error should be “Type error , assigning number to a string” or something likewise, not undefined.
How do you call “getSpreads”? Like it is, or with “await”? Usually, that undefined points to an unresolved promise (if the data is actually there).

Thank you for your help. After logging my data object, I noticed that the URL structure I was using for testing, and the one in my code were slightly different. Therefore, data . bookmakers [ 0 ]. markets [ 0 ]. outcomes [ 1 ]. point actually didn’t exist.

Once I added one small parameter to the API URL it worked.
Just a rookie mistake, using bad test data against my actual results.
I do appreciate your help though, it actually got me on the right track.

Glad you worked it out. Closing thread.