FacultyCreative / ngActiveResource

Connects business objects and REST web services for Angular.js
255 stars 34 forks source link

TypeError when setting .hasMany in my models #52

Closed DibbsZA closed 10 years ago

DibbsZA commented 10 years ago

The "Problem" model (see code below) works when it is stand-alone, but as soon as i attempt to establish a "this.hasMany" relationship with the "Activity" model I get the following error.

  TypeError: Cannot set property 'problem' of undefined
  at Function.<anonymous> (http://localhost:8000/lib/js/ngActiveResource/ng-active-resource.js:1669:50)
    at http://localhost:8000/lib/js/lodash/lodash.js:912:25
    at Function.forEach (http://localhost:8000/lib/js/lodash/lodash.js:3307:15)
    at Function._this.new (http://localhost:8000/lib/js/ngActiveResource/ng-active-resource.js:1667:13)
    at Function.Associations.get (http://localhost:8000/lib/js/ngActiveResource/ng-active-resource.js:698:20)
    at transformSearchTermsToForeignKeys (http://localhost:8000/lib/js/ngActiveResource/ng-active-resource.js:1194:41)
    at generateGET (http://localhost:8000/lib/js/ngActiveResource/ng-active-resource.js:1242:32)
    at Function._this.where (http://localhost:8000/lib/js/ngActiveResource/ng-active-resource.js:1709:18)
    at Scope.$scope.load (http://localhost:8000/js/controllers.js:113:17)
    at http://localhost:8000/lib/js/angular/angular.js:10347:21 

Please can you guide me as what the problem might be (API?, primaryKey?)

This is my model definition.

    angular.module('opsCentralApp.models', ['ActiveResource'])        

        .factory('Problem', ['ActiveResource', function (ActiveResource) {

          function Problem(data) {
            this.number('ProblemID');
            this.number('PortalID');
            this.string('Title');
            this.string('Status');
            this.string('Priority');
            this.string('CreatedDate');
            this.string('ProblemStart');
            this.string('ProblemResolved');
            this.string('Category');
            this.string('Service');
            this.string('SubService');
            this.hasMany('activity');
          }

          Problem.inherits(ActiveResource.Base);
          Problem.primaryKey = "ProblemID";
          Problem.api.set('http://myurl/API/Incident');
          Problem.api.indexURL  = 'http://myurl/API/Incident/Problem';

          return Problem;
        }])

        .factory('Activity', ['ActiveResource', function (ActiveResource) {

              function Activity(data) {
                this.ActivityID   = data.ActivityID;
                this.ProblemID    = data.ProblemID;
                this.UserName     = data.UserName;
                this.UpdateTime   = data.UpdateTime;
                this.Description  = data.Description;
                this.belongsTo('problem');

              }

              Activity.inherits(ActiveResource.Base);
              Activity.primaryKey     = "ProblemID";
              Activity.api.set('http://myurl/API/Incident');
              Activity.api.indexURL   = 'http://myurl/API/Incident/Activity/[:ProblemID]';

              return Activity;

        }]);
DibbsZA commented 10 years ago

A whole week it has taken me to notice the plural form of "comment" that is used in the 'this.hasMany(comments)' as written your example code.

So when I switched to using 'activities' in my code it no longer throws that error.

The pluralizer is a quite clever but a but quite obscure (imo) and perhaps too tricky for its own good LOL