[英]Dog API breeds return undefined 404 error React.js
I'm trying to get the specific photos that belong to a dog breed when a user clicks on a breed (for instance, they click on "hound" and then the photos of 3 random hounds appear).当用户单击某个品种时,我试图获取属于某个犬种的特定照片(例如,他们单击“猎犬”,然后出现 3 只随机猎犬的照片)。 I continue to receive
GET https://dog.ceo/api/breed/undefined/images/random/3 404
(404 error and returning undefined inside of the fetch url) in the dev tools when clicking on the specific breed.单击特定品种时,我在开发工具中继续收到
GET https://dog.ceo/api/breed/undefined/images/random/3 404
(404 错误并在获取 url 中返回未定义)。 When I console.log breedName
inside of getSelectedBreed
, it comes back as the specific breed that the user clicked on however, once the interpreter gets to the method, selectBreed
, the console.log returns undefined
.当我在
getSelectedBreed
中使用 console.logbreedName 时,它会作为用户单击的特定品种返回,但是,一旦解释器到达方法breedName
,console.log selectBreed
返回undefined
。
Here is my code for my App
component and Breeds
component, thank you!这是我的
App
组件和Breeds
组件的代码,谢谢!
App
component: App
组件:
import React, { Component } from 'react';
import './App.css';
import Header from '../Header/Header';
import Breeds from '../Breeds/Breeds';
class App extends Component {
constructor() {
super();
this.state = {
breedName: '',
breeds: [],
hasErrors : false
}
}
getSelectedBreed = (breedName) => {
this.setState({
breedName: breedName,
})
this.selectBreed();
}
selectBreed = (breedName) => {
console.log('breedName:', breedName)
fetch(`https://dog.ceo/api/breed/${breedName}/images/random/3`)
.then(res => res.json())
.then(breed => this.setState({ getSelectedBreed: breed.breedName }))
.catch(() => this.setState({ hasErrors: true }))
}
componentDidMount() {
const breedUrl = 'https://dog.ceo/api/breeds/list/all'
fetch(breedUrl)
.then(res => res.json())
.then(data => this.setState({breeds: data.message}))
.catch(() => this.setState({ hasErrors: true }))
}
render() {
console.log('breed', this.state.breedName)
return (
<div>
<Header />
<Breeds
breeds={this.state.breeds}
getSelectedBreed={this.getSelectedBreed}
/>
</div>
)
}
}
export default App;
and Breeds
component:和
Breeds
组件:
import React, {Component} from 'react';
import './Breeds.css';
class Breeds extends Component {
constructor(props) {
super(props);
this.state={
}
}
render() {
const listOfBreeds = Object.keys(this.props.breeds)
const breedItems = listOfBreeds.map((breedName, index) =>
<div key={index}>
<div onClick={()=>this.props.getSelectedBreed(breedName)}>
<p className='breed-name' onClick={()=>this.props.getSelectedBreed(breedName)}>{breedName}</p>
</div>
</div>
)
return (
<div>
<div className='breed-container'>{breedItems}</div>
</div>
);
}
}
export default Breeds;
Looks like you forgot to pass bread name in selectBreed method.看起来您忘记在 selectBreed 方法中传递面包名称。
getSelectedBreed = (breedName) => {
this.setState({
breedName: breedName,
})
this.selectBreed(breedName); //Missing breedName in your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.