Search code examples
mongodbangularjsyeomanyeoman-generator

Unknown provider error when injecting factory


I am using yeoman angular full stack generator. Trying out ToDo items tutorial with MongoDB. Everything worked fine i.e. I was able to read from DB using $http.get. However I decided to go further and create a factory so I can perform CURD.

After creating factory I tried to inject it but getting error as follows:

Error: [$injector:unpr] Unknown provider: factToDoProvider <- factToDo
http://errors.angularjs.org/1.2.6/$injector/unpr?p0=factToDoProvider%20%3C-NaNactToDo
at http://localhost:9000/bower_components/angular/angular.js:78:12
at http://localhost:9000/bower_components/angular/angular.js:3538:19
at Object.getService [as get] (http://localhost:9000/bower_components/angular/angular.js:3665:39)
at http://localhost:9000/bower_components/angular/angular.js:3543:45
at getService (http://localhost:9000/bower_components/angular/angular.js:3665:39)
at invoke (http://localhost:9000/bower_components/angular/angular.js:3687:13)
at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:3708:23)
at http://localhost:9000/bower_components/angular/angular.js:6758:28
at link (http://localhost:9000/bower_components/angular-route/angular-route.js:897:26)
at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:6212:13) 

Main.js controller looks like

'use strict';

angular.module('angularFsApp')
.controller('MainCtrl', function ($scope, $http, factToDo) {
    $http.get('/api/awesomeThings').success(function(awesomeThings) {
      $scope.awesomeThings = awesomeThings;
    });

    $http.get('/todo/todoItems').success(function(todoItems) {
      $scope.todoItems = todoItems;
    });
    //$scope.newtodoItems = factToDo.getAllItems();

   }); 

Where factToDo is my factory which look like as follows (todo.js)

angular.module('angularFsApp')
.factory('factToDo', function() {
    return {
        getAllItems: function () {
            return [
                {description: 'Hello World 1', priority: '15'},
                {description: 'Hello World 2', priority: '15'},
                {description: 'Love for all', priority: '1500'}
            ];
            }
    }
});

I tried by changing my code in main.js as described in AngularJS error ref as follows

angular.module('angularFsApp')
.controller('MainCtrl', ['$scope', '$http', 'factToDo', function ($scope, $http, factToDo) {

as well as I tried Dan Wahlin but i got same issue.


Solution

  • Make sure the file with the 'factToDo' is included into your app.

    For a convenient development and to avoid issues like this in the future try the Grunt task runner to concatenate all your code for you and include it as a one file.

    This tutorial seems to be sufficient for starting with Grunt and file concatenation.