2

I am trying to learn AngularJS. Whdile I was following the instructor, I wrote the same code as he did. But I am getting a [$injector:undef] Provider 'dataService' must return a value from $get factory method error. When I searched for this error in web, it is told that I must return a function or an object. I guess I am doing it. My factory declaration is below:

module.factory("dataService", ['$http', '$q', function ($http, $q) {
    var _topics = [];
    var _getTopics = function () {
        var deferred = $q.defer();
        $http.get("/api/v1/topics?includeReplies=true")
            .then(function (result) {
                //success
                angular.copy(result.data, _topics);
                deferred.resolve();
            },
            function () {
                //error         
                deferred.reject();
            });
        return deferred.promise;
    };
    return
    {
        topics: _topics;
        getTopics: _getTopics;
    };
}]);

Any helps are appreciated...

Wayne Ellery
  • 7,758
  • 1
  • 28
  • 44
user007
  • 1,190
  • 2
  • 14
  • 38

1 Answers1

9

There's a syntax error in you code in the return statement. You should use a comma to separate the properties of the object. It should be:

return {
    topics: _topics,
    getTopics: _getTopics
};

Based on the comment thread, the ultimate solution appears to have been removing the line break between return and {. See this answer for more explanation.

Community
  • 1
  • 1
Wayne Ellery
  • 7,758
  • 1
  • 28
  • 44
  • I tried this before. It gives me the below error `Critical error was detected at line 35, column 18 in http://localhost:50272/js/home-index.js. SCRIPT1004: Expected ';' Unhandled exception at line 4117, column 9 in http://localhost:50272/Scripts/angular.js 0x800a139e - JavaScript runtime error: [$injector:modulerr] Failed to instantiate module homeIndex due to: Error: [$injector:nomod] Module 'homeIndex' is not available!` – user007 Dec 28 '14 at 04:12
  • Please post what's on the line. You should be able to click on it. – Wayne Ellery Dec 28 '14 at 04:14
  • It is exactly on the "," on the code `return { topics: _topics, getTopics: _getTopics };` – user007 Dec 28 '14 at 04:16
  • 3
    Ignore my edit. Try moving the starting brace up onto the return line. – Wayne Ellery Dec 28 '14 at 04:22
  • Syntax is definitely correct: http://plnkr.co/edit/D3a53Jg3T0iyURWQJ3aW?p=preview – Wayne Ellery Dec 28 '14 at 04:23
  • Is that fixed now. If so please mark my answer as correct. And can you please change your question back to what it was before. – Wayne Ellery Dec 28 '14 at 04:49
  • you are right. The syntax was wrong. The second one is the ";". But the first one is the flower bracket after return. It should be on the same line. Go to your plunker and press "enter" between return and {. Thanks for the helps – user007 Dec 28 '14 at 04:49