whatadewitt / yahoo-fantasy-sports-api

NodeJS wrapper for the Yahoo! Fantasy Sports API
https://yahoo-fantasy-node-docs.vercel.app/
MIT License
192 stars 52 forks source link

Map Error on yf.user.game_teams #49

Closed dbalders closed 5 years ago

dbalders commented 5 years ago

Hey,

When trying to get the user.game_teams information, I am getting the following error:

./node_modules/yahoo-fantasy/helpers/teamHelper.mjs:7
team.team_logos = team.team_logos.map(logo => logo.team_logo);
TypeError: Cannot read property 'map' of undefined

Code:

yf.user.game_teams('385', function(err, data) {
      console.log(err)
      console.log(data)
})

If I change game_teams to game_leagues I have no problems. Seems to just be this one endpoint. Looks like it is also broken on the yfantasysandbox.herokuapp.com.

whatadewitt commented 5 years ago

Thanks for this! I'll check it out as soon as I get some time!

whatadewitt commented 5 years ago

@dbalders hmmm... this may be an NBA thing... what happens when you run it in the sandbox here: http://yfantasysandbox.herokuapp.com/resource/user/game_teams and put "385" into it?

whatadewitt commented 5 years ago

@dbalders like everything, I ask for some help, then I find the issue. should be a pretty easy fix. just want to make sure i've got it fixed...

dbalders commented 5 years ago

@whatadewitt hey, the site just spins. So, I put a console.log(team) in line 6 of the teamHelper.mjs. I noticed it looks like somehow the method gets called twice. The first time has the data, the 2nd time its undefined and fails.

Like so:

{ team_key: '[teamkey]',
  team_id: '1',
  name: '[name],
  is_owned_by_current_login: 1,
  url: '[url],
  team_logos: [ { team_logo: [Object] } ],
  waiver_priority: 9,
  faab_balance: '100',
  number_of_moves: '31',
  number_of_trades: 0,
  roster_adds: { coverage_type: 'week', coverage_value: 17, value: '2' },
  league_scoring_type: 'head',
  has_draft_grade: 0,
  managers: [ { manager: [Object] } ] }
{}
undefined
(node:1963) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'map' of undefined
    at mapTeam (/Users/[computer]/GitHub/fantasyapi/node_modules/yahoo-fantasy/helpers/teamHelper.mjs:9:39)
    at mapUserTeams (/Users/[computer]/GitHub/fantasyapi/node_modules/yahoo-fantasy/helpers/userHelper.mjs:52:20)
    at yf.api.then.data (/Users/[computer]/GitHub/fantasyapi/node_modules/yahoo-fantasy/resources/userResource.mjs:69:23)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
(node:1963) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1963) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I am not sure why it is called twice, but if I wrap it in an if statment like this, it removes the error and stops the app from crashing

export function mapTeam(t) {
  const team = mergeObjects(t);
  // clean up team_logos
  if (team) {
    team.team_logos = team.team_logos.map(logo => logo.team_logo);

    // clean up managers
    team.managers = team.managers.map(manager => manager.manager);

    return team;
  }
}
whatadewitt commented 5 years ago

oh my... this is much different from what i was seeing... and I can't seem to duplicate. can you get me the entire "data" object after putting a "console.log(data)" on line 68 of userResource.mjs?

Thanks!

dbalders commented 5 years ago

{ fantasy_content: { 'xml:lang': 'en-US', 'yahoo:uri': '/fantasy/v2/users;use_login=1/games;game_keys=385/teams', users: { '0': [Object], count: 1 }, time: '58.022975921631ms', copyright: 'Data provided by Yahoo! and STATS, LLC', refresh_rate: '60' } } { team_key: '385.l.40083.t.1', team_id: '1', name: '', is_owned_by_current_login: 1, url: '', team_logos: [ { team_logo: [Object] } ], waiver_priority: 9, faab_balance: '100', number_of_moves: '31', number_of_trades: 0, roster_adds: { coverage_type: 'week', coverage_value: 17, value: '2' }, league_scoring_type: 'head', has_draft_grade: 0, managers: [ { manager: [Object] } ] }

whatadewitt commented 5 years ago

Weird... I don't know what would make it run twice... Nothing I do makes it do that. Do you have the code anywhere that I can take a look?

dbalders commented 5 years ago

huh, so weird. Let me try and make this as easy as possible rather than linking you to a large project to just sift through. This is the function I am calling, i double checked everything and it is only called once.

function getLoginInfo(gameKey) {
    // console.log(gameKey)
    console.log("here")
    yf.user.game_teams(
        gameKey, 
        function(err, data) {
        //   if (err)
            // handle error
            console.log('in here')
            console.log(err + " /////////////error")
            console.log(data + " //////////data")
        }
      );
}

Then, I added in some console logs to the yahoo-fantasy teamHelper.mjs:

export function mapTeam(t) {
  const team = mergeObjects(t);
  // clean up team_logos
  console.log(team.team_logos.length + ' /////////length')
  console.log(team)

  if (team.team_logos.length) {
    console.log('in team')
    team.team_logos = team.team_logos.map(logo => logo.team_logo);
  } else {
    // fix issue #49 -- no team logo throwing error
    console.log('in else')
    team.team_logos = [];
  }

  // clean up managers
  team.managers = team.managers.map(manager => manager.manager);

  console.log('team again')
  console.log(team)

  return team;
}

And this is the full console out:

here
1 /////////length
{ team_key: '',
  team_id: '1',
  name: '',
  is_owned_by_current_login: 1,
  url: '',
  team_logos: [ { team_logo: [Object] } ],
  waiver_priority: 9,
  faab_balance: '100',
  number_of_moves: '31',
  number_of_trades: 0,
  roster_adds: { coverage_type: 'week', coverage_value: 17, value: '2' },
  league_scoring_type: 'head',
  has_draft_grade: 0,
  managers: [ { manager: [Object] } ] }
in team
team again
{ team_key: '',
  team_id: '1',
  name: '',
  is_owned_by_current_login: 1,
  url: '',
  team_logos: 
   [ { size: 'large',
       url: 'https://ct.yimg.com/cy/1489/24973285869_03532bc369_192sq.jpg?ct=fantasy' } ],
  waiver_priority: 9,
  faab_balance: '100',
  number_of_moves: '31',
  number_of_trades: 0,
  roster_adds: { coverage_type: 'week', coverage_value: 17, value: '2' },
  league_scoring_type: 'head',
  has_draft_grade: 0,
  managers: 
   [ { manager_id: '1',
       nickname: 'David',
       guid: 'S44IT3T2IGACIYI474XIH5LSWU',
       is_commissioner: '1',
       is_current_login: '1',
       email: '',
       image_url: 'https://ct.yimg.com/cy/1768/39361574426_98028a_64sq.jpg' } ] }
in here
TypeError: Cannot read property 'length' of undefined /////////////error
undefined //////////data
(node:2551) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
    at mapTeam (/Users/[computer]/GitHub/fantasyapi/node_modules/yahoo-fantasy/helpers/teamHelper.mjs:7:39)
    at mapUserTeams (/Users/[computer]/GitHub/fantasyapi/node_modules/yahoo-fantasy/helpers/userHelper.mjs:52:20)
    at yf.api.then.data (/Users/[computer]/GitHub/fantasyapi/node_modules/yahoo-fantasy/resources/userResource.mjs:69:23)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
(node:2551) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2551) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So, seems the error is getting sent back to the original function before throwing the error again in node. Maybe thats why i thought it was happening twice. Doesn't look like it actually is. Does this help at all?

Not sure why it is throwing 'Cannot read property 'length' of undefined'

dbalders commented 5 years ago

oh my gosh i think i figured it out. I had a 2nd blank team on yahoo that they never assigned to a league. So i think it was running the loop twice seeing i had 2 teams but the other team didn't have a league attached so was erroring out.

It was running this code twice since I had two teams but wasn't in two leagues:

https://github.com/whatadewitt/yfsapi/blob/master/helpers/userHelper.mjs#L51-L55

dbalders commented 5 years ago

Yep, everything is working completely as expected. Man, I am so sorry for wasting your time. I literally didn't even think about it cause it wasn't a 2nd team I ever thought about since yahoo never assigned it anywhere. The second I deleted that team everything worked. Thanks for the help, sorry again.

whatadewitt commented 5 years ago

Lol! Interesting use case!! Glad it's resolved!