lilRed
lilRed

Reputation: 55

how i get the value from the first url and pass that value to second url parameter?

I am new to learn express. i try to get the value from the first url and pass that value to second url parameter?

router.get('/',  function(req, res, next){

const teamId = ''
var player_data = {
method: 'GET',
url: 'https://sofascore.p.rapidapi.com/teams/get-player-statistics',
params: {teamId: '', tournamentId: '7', seasonId: '29267', type: 'overall'},
};

axios.request(team_data)
  .then(function (response) {
    const teamId = response.data.teams[0].id
    console.log(teamId)
    //axios.request(player_data)
    //   .then(function (response){
    //     const players = response.topPlayers.goals
    //     res.render('index',{players: players})
    //   })
     })          
})

the output

 [nodemon] restarting due to changes...
 [nodemon] starting `node ./bin/www`
 [nodemon] restarting due to changes...
 [nodemon] starting `node ./bin/www`
 3427

it returns that team to me correctly, but why I pass the id to second url to get team member nothing there?

output
    params: {
    teamId: '',
    tournamentId: '7',
    seasonId: '29267',
    type: 'overall'
  },

  ....
           'user-agent': [ 'User-Agent', 'axios/0.21.4' ],
    host: [ 'Host', 'sofascore.p.rapidapi.com' ]
  }
},
data: '400 - Bad Request'
},
 isAxiosError: true,
  toJSON: [Function: toJSON]

Upvotes: 1

Views: 126

Answers (1)

Barmar
Barmar

Reputation: 782683

You need to put teamId into the player_data.params object.

axios.request(team_data)
  .then(function(response) {
    const teamId = response.data.teams[0].id
    console.log(teamId)
    player_data.params.teamId = teamId
    axios.request(player_data)
      .then(function(response) {
        const players = response.topPlayers.goals
        res.render('index', {
          players: players
        })
      })
  })
})

Upvotes: 1

Related Questions