Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS Basics (1.x) Improving Our Todo App Finalizing Our Application

Anthony c
Anthony c
20,907 Points

Getting todos is undefined

the ''' is not working for making a code block, so moderators can feel free to edit this post...

ReferenceError: todos is not defined
    at saveTodos (http://port-80-scyozibbj0.treehouse-app.com/scripts/services/data.js:29:17)
    at Scope.$scope.saveTodos (http://port-80-scyozibbj0.treehouse-app.com/scripts/controllers/main.js:31:17)
    at fn (eval at <anonymous> (http://port-80-scyozibbj0.treehouse-app.com/vendor/angular.js:13231:15), <anonymous>:4:311)
    at callback (http://port-80-scyozibbj0.treehouse-app.com/vendor/angular.js:23411:17)
    at Scope.$eval (http://port-80-scyozibbj0.treehouse-app.com/vendor/angular.js:15916:28)
    at Scope.$apply (http://port-80-scyozibbj0.treehouse-app.com/vendor/angular.js:16016:25)
    at HTMLAnchorElement.<anonymous> (http://port-80-scyozibbj0.treehouse-app.com/vendor/angular.js:23416:23)
    at HTMLAnchorElement.eventHandler (http://port-80-scyozibbj0.treehouse-app.com/vendor/angular.js:3293:21)

My code:

data.js
'use strict';

angular.module("todoListApp")


.service('dataService', function($http) {

  this.helloConsole = function(){

    console.log('This is the hello console service!');

  };

  this.getTodos = function(callback) {
  $http.get('mock/todos.json')
  .then(callback)

   };



  this.deleteTodo = function(todo){
    console.log("The " + todo.name + " todo has been deleted!")

  };

  this.saveTodos = function(todo){
    console.log(todos.length + " todo have been saved!")

  };


 }); 
main.js
'use strict';


angular.module("todoListApp")
.controller('mainCtrl', function($scope, dataService) {
  $scope.addTodo = function() {
    var todo = {name: "This is a new todo." };
    $scope.todos.unshift(todo);
  };

  $scope.helloWorld = dataService.helloWorld;

  dataService.getTodos(function(response){
    console.log(response.data);
    $scope.todos = response.data;

   });

  $scope.deleteTodo = function(todo, $index) {
    dataService.deleteTodo(todo);
    $scope.todos.splice($index, 1);

  };

    $scope.saveTodos = function(){
      var filteredTodos = $scope.todos.filter(function(todo) {      
        if(todo.edited) {
          return todo;
        };        
      });      
    dataService.saveTodos(filteredTodos);
    };    
});

Hi Anthony c, just letting you know that I've updated your post with the correct code block format.

It's actually a set of three backticks ``` (the key is usually just left of the number 1 on your keyboard), not three apostrophes '''

1 Answer

this.saveTodos = function(todo){ 
  console.log(todos.length + " todo have been saved!") 
}

You have never defined todos in your service. It looks like you want to be able to save multiple todos with the saveTodos function. You might want to change the parameter in the function from todo to todos.

this.saveTodos = function(todos){
  console.log(todos.length + " todo have been saved!")
}