José Bejerano José Bejerano - 24 days ago 13
Node.js Question

AngularJS:12330 TypeError: Cannot read property '$promise' of undefined

I am stuck in this issue and after reading a lot of answers, blogs and articles I wasn't able to figure it out, what's wrong with my code.

This is a modal for inserting functionalities

Controller:

(function () {
'use strict';
angular
.module('nameSearch')
.controller('ModalPPNameSearchController', ModalPPNameSearchController);

ModalPPNameSearchController.$inject = ['$modalInstance','mdOptions','$window','nameSearchService','pendingPrior','nsCommonService'];

function ModalPPNameSearchController($modalInstance, mdOptions,$window,nameSearchService,pendingPrior,nsCommonService) {
var vm = this;

debugger;
vm.pendingPriorReason = [];
vm.pendingPriorReasons = [];
loadPendingPriorReasons();

if (!angular.isDefined(pendingPrior)) {
pendingPrior = pendingPriorEmpty;
}
vm.pendingPrior = pendingPrior;
vm.title = "Add New";

if (mdOptions.action == 'edit') {
vm.title = "Edit: " + vm.pendingPrior.code;
}

function pendingPriorEmpty (){
return {
leadId: '',
firstName: '',
lastName: '',
state: ''
};
}
function loadPendingPriorReasons() {
var tmp = nsCommonService.get();
console.log(tmp);
tmp.$promise.then(getGetPendingPriorReasonsCompleted, angularErrorHandler);
}

function getGetPendingPriorReasonsCompleted(pendingPriorReason) {
console.log(pendingPriorReason);
vm.pendingPriorReasons = pendingPriorReason;
}
vm.save = function () {
debugger;
}
vm.cancel = function () {
$modalInstance.dismiss('cancel');
}
}
})();

Service:
The service is getting the data from the web api.
(function () {
'use strict';
angular.module('nameSearch')
.factory('nsCommonService', nsCommonService);

nsCommonService.$inject = ['$resource'];
function nsCommonService($resource) {

var commonResourcePendingPrior = $resource('/api/ppreasons/:reasonId');

var service = {
get: get,
getPendingPriorStatus: GetPendingPriorStatus
};
return service;
///////////////////////////////////////////
function get() {
commonResourcePendingPrior.query();
}

function GetPendingPriorStatus() {
return '123';
}
}
}());


I would some advices, because I think am doing something wrong.
Thanks

Answer

You need to return the promise from get()

function get() {   
  return commonResourcePendingPrior.query();                     
}
Comments