Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm getting this error: "TypeError -- cannot set property 'select' of undefined" in the promise for a repository function.

The variable obj contains an array of objects.

.then(function (obj) {
   // this works...
   var abc = {};
   abc.title = "hello";
   abc.obj = obj;

   // this doesn't work...
   $scope.modelData = {};
   $scope.title = "Import";
   $scope.message = "Please select a Purchase Request"
   $scope.select = obj; // <-- TypeError -- cannot set property 'select' of undefined'

It's raising the error during an angular function so I'm sure it's Angular that doesn't like it.

=== Edited to include more code.

Here's the relevant bits of my controller:

var app = ipoModule.controller("ipoController", 
                               function ($scope, $compile, $window, $http, $q, $log, ipoRepository, $routeParams, limitToFilter, $modal, ngTableParams) {

  $scope.ipoRepository = ipoRepository;

  //... not-relevant stuff

  $scope.PRList = ipoRepository.getMvpPRs($scope.POHeader.warehouseId)
            .then(function (obj) {
                var modelData = {};
                modelData.title = "MVP Import";
                modelData.message = "MVP Purchase Request ID";
                modelData.select = obj;
                $scope.modalData = modelData;

                // then do more stuff but it breaks before that...
            })
        .catch(function (err) {
            $scope.HandleErrorDoc('Search Error', err)
        });
})

  

and here's some of my repository

ipoModule.factory('ipoRepository', function($resource, $http) {

  var genericGet = function(params, URL) {
    return $http.get(URL, {
      params: params
    }).then(function(res) {
      if (res.data) {
        return res.data;
      } else {
        return null;
      }
    });
  };

  return {
    getSearchResults: function(txt, chk, myPO) {
      return genericGet({
        SearchText: txt,
        excludeCompleted: chk,
        MyPO: myPO
      }, '/Search/GetSearchResults');
    },

    getMvpPRs: function(id) {
      return genericGet({
        id: id
      }, '/MvpUtility/GetMvpPRs')
    }
  }
});

... and now the module ...

var ipoModule = angular.module('ipoModule', ['ngRoute', 'ngResource', 'ngGrid', 'ui.bootstrap', 'unsavedChanges', 'ngIdle', 'ngTable'])
  .config(function($routeProvider, $locationProvider, unsavedWarningsConfigProvider) {
    //$locationProvider.html5Mode(true);
    unsavedWarningsConfigProvider.useTranslateService = false;
  });

share|improve this question
    
Can you add a larger code snippet? Is $scope being injected? – joemfb Apr 2 '15 at 16:54
    
Sounds like $scope is undefined. Show us the rest of your controller – Tom Apr 2 '15 at 16:59
    
could you please add console.log(obj) ? Something is problem in obj. – Samir Apr 2 '15 at 17:10

I came to the conclusion that Angular couldn't figure out how to handle an array of objects so I tried to create the object as a regular javascript variable first and then assign it to a scope variable ... and it worked.

I'll leave this unanswered though because there may be an "angular" way to do this which I am not aware of.

        $scope.PRList = ipoRepository.getMvpPRs($scope.POHeader.warehouseId)
            .then(function (obj) {
                var modelData = {};
                modelData.title = "MVP Import";
                modelData.message = "MVP Purchase Request ID";
                modelData.select = obj;
                $scope.modalData = modelData;

and here's a sample of what is returned in my obj variable:

[
  {
    "prid": "0000",
    "description": "PR0000 - j11-v1 test - youngm Jan 11"
  },
  {
    "prid": "0001",
    "description": "PR0001 - j11-v1 test - youngm Jan 11"
  },
  {
    "prid": "0004",
    "description": "PR0004 - j11-v1 test - j20-contractor Jan 20"
  },
  {
    "prid": "0005",
    "description": "PR0005 - tigerdirect  - yeungm Mar 01"
  }
]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.