title: TypeError: Cannot read property '$promise' of undefined with using ngResource in angularJS
body: <p>I created a service in my angularJS app with ngResource to send some data to my rest api.</p>
  
  <p>I want to use a promise to perform some tasks with it in my controller just after I sent a request.</p>
  
  <p>Here is my service :</p>
  
  <pre><code>angular.module("MyService", ['ngResource'])
  
  .factory("myService",  function ($rootScope, $http, $resource) {
  
      var apiData = $resource(
          "/api", {},
          {
  
                    "postQuestion": { method: "POST", url: "/api/:userid"}
  
          });
  
          return {
  
              postQuestion: function(question,  user_id) {
                  apiData.postQuestion({ userid: user_id}, question, function(data) {
                      console.log("Sucsess !");
                  }, function(error) {
                      console.log("Error " + error.status + " when sending request : " + error.data);
                  });
  
              }
      }
  });
  </code></pre>
  
  <p>And here is how I used it in my controller :</p>
  
  <pre><code>app.controller("MyCtrl", function ($scope, $filter,  $http, $sce, myService, ngDialog, $rootScope, $location) {
  
  
  $scope.postQuestion = function(newQuestion)
  {
  
      myService.postQuestion(newQuestion, 1).$promise.then(function(){
          $scope.showNewQuestion = false;
          $location.path("/");
      });
  }
  
  
  })
  </code></pre>
  
  <p>But when running my app, I got this error in my console :</p>
  
  <blockquote>
    <p>TypeError: Cannot read property  '$promise'  of undefined
        at Scope.$scope.postQuestion ...</p>
  </blockquote>
  
  <p>Don't know why, please help</p>