velua velua - 1 month ago 11
AngularJS Question

Cannot read property 'finally' of undefined

I can't figure out why I'm getting an

Cannot read property 'finally' of undefined error here..

$scope.saveToDos = function(){
console.log("Save to do was pressed.")
var filteredTodos = $scope.todos.filter(function(todo){
if(todo.edited){
return todo
}
})
console.log("There are " + filteredTodos.length + " edited todos")
dataService.saveToDos(filteredTodos)
.finally($scope.resetTodoState())
}


It errors out on the finally line and I'm not sure why.
The issue can be reproduced cloning Git repo
https://github.com/Velua/To-Do-List

this.saveToDos = function(todos){
var queue = [];
todos.forEach(function(todo){
var request;
if(!todo._id){
request = $http.post('/api/todos', todo);
} else{
request = $http.put('/api/todos/' + todo._id, todo).then(function(result){
todo = result.data.todo;
return todo
})
}
queue.push(request);
})
$q.all(queue).then(function(results){
console.log("I saved " + todos.length + " todos!");
})
}


Thanks!

Answer

You aren't returning anything from saveToDos(), so the return value is by default undefined.

You probably wanted to return $q.all(...