1

In my meanjs app created using meanjs generator and auto generated CRUD-module, for income below, I want to customize the promise returned by the get method so that I can call methods and do transformations on the retrieved data. So i attempt to change it in my controller:

// Find existing Income
    $scope.findOne = function() {
        Incomes.get({
            incomeId: $stateParams.incomeId
    }).then(function(income){
       $scope.income = income;
       $scope.incomeDollar= income*$scope.dollarRate;
    });

This only gives me the error : undefined is not a function, and pointing on then. What am i doing wrong here? How can I do transformations on the data retrieved from the get method above?

The pattern i am trying to use is in the end of the article here. I want to switch from the short version to the long so that i can do more stuff in my callback.

// LONG:

myModule.controller('HelloCtrl', function($scope, HelloWorld) {

  HelloWorld.getMessages().then(function(messages) {
    $scope.messages = messages;
  });

});

To simplify this, the writer of the article place the promise returned by ‘getMessages’ on the scope:

// SHORTER:

myModule.controller('HelloCtrl', function($scope, HelloWorld) {

  $scope.messages = HelloWorld.getMessages();

});
David Karlsson
  • 9,396
  • 9
  • 58
  • 103
  • Could you post the definition of Incomes service? – udalmik Dec 07 '14 at 18:32
  • I am using an abstract workflow, the Incomes service have not been customized for get, only put. As shown in [this template](https://github.com/meanjs/generator-meanjs/blob/master/crud-module/templates/angular-module/services/_.client.service.js) that has been used – David Karlsson Dec 07 '14 at 18:43
  • The shorter way of getMessages is no longer available in Angular (1.3), check out: https://docs.angularjs.org/guide/migration – Walter Brand Dec 07 '14 at 19:14

1 Answers1

2

The promise object can be accessed using $promise. From the doc

The Resource instances and collection have these additional properties:

$promise: the promise of the original server interaction that created this instance or collection.

On success, the promise is resolved with the same resource instance or collection object, updated with data from server...

So, your code should look like:

// Find existing Income
$scope.findOne = function() {
    Incomes.get({
        incomeId: $stateParams.incomeId
}).$promise.then(function(income){
   $scope.income = income;
   $scope.incomeDollar= income*$scope.dollarRate;
});
Trojanware
  • 84
  • 1
  • 2