I have to axios calls in this file currently, both are exported and mutate state where they're used.
(目前,我必须在此文件中使用axios调用,无论是导出还是使用它们的状态都会发生变化。)
The first one works perfectly, the second one I'm having this error(第一个工作正常,第二个我遇到此错误)
export function getPlayerByName(playerFirstName, playerLastName) {
axios({
"method": "GET",
"url": `https://api-nba-v1.p.rapidapi.com/players/firstName/${playerFirstName}`,
"headers": {
"content-type": "application/octet-stream",
"x-rapidapi-host": "api-nba-v1.p.rapidapi.com",
"x-rapidapi-key": "4cec6170bcmsh5d6a0ea78315a5ep10f15cjsn59ef0231e8e4"
}
})
.then((playerList) => {
console.log('PLAYERS WITH THIS FIRST NAME');
console.log(playerList.data.api.players)
// * Then Get list of players with first name that also match provided last name
console.log('ABOUT TO GET PLAYERS WITH THIS FIRST NAME AND LAST NAME');
var result = playerList.data.api.players.filter(players => {
return players.lastName === playerLastName;
})
console.log('RESULT OF FINDING LAST NAME: ', result);
console.log(`THE PLAYER WITH THIS FULL NAME `, result[0]);
// * Save Player In state For Use
this.setState({
currentPlayer: result[0]
})
console.log(this.state.currentPlayer.country);
console.log(this.state.currentPlayer.dateOfBirth);
this.getPlayerTeam(this.state.currentPlayer.teamId)
})
.catch((error) => {
console.log(error)
})
}
(})
// The above behaves as expected but below does not.
(//上面的行为符合预期,但下面的行为不符合预期。)
However, when using the below in componentdidmount of it's file, it works again.(但是,在文件的componentdidmount中使用以下内容时,它将再次起作用。)
But I was told it was more structured to organize my axios calls in another file so I want this to work(但是我被告知,它更结构化地将axios调用组织在另一个文件中,所以我希望它能正常工作)
export const getAllNBAPlayers = () => {
// Retrieve list of all players cin NBA history and save it in state
axios({
"method": "GET",
"url": "https://api-nba-v1.p.rapidapi.com/players/country/USA",
"headers": {
"content-type": "application/octet-stream",
"x-rapidapi-host": "api-nba-v1.p.rapidapi.com",
"x-rapidapi-key": "4cec6170bcmsh5d6a0ea78315a5ep10f15cjsn59ef0231e8e4"
}
})
.then((response) => {
// Should be array of ALl players in NBA history
// console.log(response.data.api.players)
let playersAsArray = response.data.api.players
console.log(playersAsArray); works fine
// Get list of First Name of all players for suggestion box
let playerNames = playersAsArray.map(function (player) {
// return List of player full names
// TODO Would like space between names but need to figure how to make
// Input component ignore spaces or create a new component
return `${player.firstName} ${player.lastName}`;
});
console.log(Array.isArray(playerNames));works fine
console.log(playerNames); //works fine
this.setState({
// allPlayers: response.data.api.players
allPlayers: playerNames
})
console.log(`PLAYERS IN STATE: `, this.state.allPlayers);
})
.catch((error) => {
console.log(error)
})
}
(})
ask by Alexander H translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…