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() {
console.log('Fetching tasks...')
const { data } = await axios.get(api + '/api/tasks')
await this.setState({ tasks: data })
try {
const { data } = await axios.get('/api/tasks')
return data
} catch (error) {
console.log(error)
this.setState({ error })
}
}
async fetchProjects() {
console.log('Fetching projects...')
const { data } = await axios.get(api + '/api/projects')
await this.setState({ projects: data })
try {
const { data } = await axios.get('/api/projects')
return data
} catch (error) {
console.log(error)
this.setState({ error })
}
}
async fetchData() {
await this.fetchTasks()
await this.fetchProjects()
await this.setState({ loading: false })
await console.log(
'Ok, everything should be loaded now and nothing should render after this without input!'
)
const tasks = await this.fetchTasks()
const projects = await this.fetchProjects()
await this.setState({ loading: false, tasks, projects })
}
componentDidMount() {