added error handling for data fetching

This commit is contained in:
notnull 2019-06-22 07:28:32 -04:00
parent 274ca42e7a
commit a098f79c27

View File

@ -35,24 +35,29 @@ class App extends React.Component {
} }
async fetchTasks() { async fetchTasks() {
console.log('Fetching tasks...') try {
const { data } = await axios.get(api + '/api/tasks') const { data } = await axios.get('/api/tasks')
await this.setState({ tasks: data }) return data
} catch (error) {
console.log(error)
this.setState({ error })
}
} }
async fetchProjects() { async fetchProjects() {
console.log('Fetching projects...') try {
const { data } = await axios.get(api + '/api/projects') const { data } = await axios.get('/api/projects')
await this.setState({ projects: data }) return data
} catch (error) {
console.log(error)
this.setState({ error })
}
} }
async fetchData() { async fetchData() {
await this.fetchTasks() const tasks = await this.fetchTasks()
await this.fetchProjects() const projects = await this.fetchProjects()
await this.setState({ loading: false }) await this.setState({ loading: false, tasks, projects })
await console.log(
'Ok, everything should be loaded now and nothing should render after this without input!'
)
} }
componentDidMount() { componentDidMount() {