I'm using vuex alongside Vue.js + Django. It's my first foray into reactive front-end development.
I'm using a Django REST setup, and wanted to emulate a server error or connection problem, and see how to handle that. So I modified my ViewSet to look like this (return 0/ or Response 403). Here's how it looks:
class ToDoViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows messages to be viewed or edited.
"""
queryset = ToDoItem.objects.all()
serializer_class = ToDoItemSerializer
# return 0 instead of a Response object for testing purposes
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
instance_id = instance.id
# self.perform_destroy(instance)
return 0
# return Response(data={'id': instance_id}, status=status.HTTP_200_OK)
I'm using axios to make the DELETE
call:
/// api is mapped to an axios.create object
deleteToDoItem(ToDoItemId) {
return api.delete(todos/${ToDoItemId}
)
.then(response => response.data)
}
I have the following in my vuex store.js:
actions: {
....
deleteToDoItem(context, toDoItemId) {
messageService.deleteToDoItem(toDoItemId).then(response => context.commit('REMOVE_TODO', response));
},
And the following in my toDo Vue component:
import { mapActions } from 'vuex';
export default {
name: 'todo-item',
props: ['todo'],
methods: {
...mapActions([
'deleteToDoItem',
]),
/// DeleteToDo uses the mapped action 'deleteToDoItem'
deleteToDo(todo_id) {
this.deleteToDoItem(todo_id).then(response => this.$store.commit('REMOVE_TODO', response)).catch((e => console.log(e)));
}
},
}
I thought the catch
in DeleteToDo in the component should suffice, but I'm still getting this error:
Uncaught (in promise) Error: Request failed with status code 500
If I add the following in the axios call:
deleteToDoItem(ToDoItemId) {
return api.delete(`todos/${ToDoItemId}`)
.then(response => response.data).catch(e => e)
}
Then I don't get the the Uncaught Promise error above, but this seems redundant. Do I have to catch twice? And if I do write it twice, how do I pass the e
from the axios call down to the action so I can discern what to do?