Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a AngularJS-based frontend using restangular to get objects. In DB one table stores Users and reference to selected language (languageId), and other contains Languages.

My problem is that everytime I try to open view to edit my User I am getting an error: TypeError: Cannot read property 'parentResource' of undefined, which does not happen when I try editing user with no language selected (null in language in Users table).

Interesting is that when I look into network tab in my browser console there is all the data needed in the response: userId, login, firstName, lastName and _embedded which contains languageId and some links between tables.

in my user controller I am getting data:

var original = userRest;

where userRest is created in app.js:

.state('users.edit', {
    (...)
    resolve: {
      userRest: function(Restangular, $stateParams) {
        return Restangular.one('users', $stateParams.userId).get();
      }
    }
  })

everything works if user has no specified language, but DB seems to be ok, all links between tables are correct, there are no problems with inserts/deletes/updates. So I think it may be because of the language data being embedded but still I cannot find a way to make it work.

Edit: Response language not null:

    {"firstName":"Test","lastName":"User","login":"testUser","password":"ttt","lastLoginSuccess":null,"lastLoginError":null,"lastDevice":null,"createdBy":"testuser18","createdOn":{"date":"2014-07-21 08:00:00","timezone_type":3,"timezone":"Europe\/Berlin"},"updatedBy":null,"updatedOn":null,"active":true,"userId":139,"_embedded":{"language":{"languageId":"en_GB","_links":{"self":{"href":"http:\/\/host\/api\/languages\/en_GB"}}}},"_links":{"self":{"href":"http:\/\/host\/api\/users\/139"}}}

response with null language:

{"firstName":"Test","lastName":"User","login":"testUser","password":"ttt","lastLoginSuccess":null,"lastLoginError":null,"lastDevice":null,"createdBy":"testuser18","createdOn":{"date":"2014-07-21 08:00:00","timezone_type":3,"timezone":"Europe\/Berlin"},"updatedBy":null,"updatedOn":null,"active":true,"userId":139,"language":null,"_links":{"self":{"href":"http:\/\/host\/api\/users\/139"}}}

Controller:

    app.controller('EditUserCtrl', ['$scope', 'Acl', 'User', '$rootScope', 'userRest', 'flash', '$q', 'Languages',
function($scope, Acl, User, $rootScope, userRest, flash, $q, Languages) {

$scope.availableLanguages = Languages.all;

    var original = userRest;
    console.info(original);
$scope.user = User.copy(original.data);
(...)
share|improve this question
    
Can you post more code of your controller and response json your getting from server? –  dhavalcengg Jul 21 '14 at 8:59
    
Added the response, whole code has over 300lines if not more, but at very beginning it fails so posted only few because they matter. if there is any language original.data is undefined. –  Chaki Jul 21 '14 at 9:22
    
Why you are doing original.data..? Your response should be in original itself, until and unless you are not doing setFullResponse. –  dhavalcengg Jul 21 '14 at 10:18
    
Oh, ok, forgot that. Yes, i do setFullResponse - I need it for other modules RestangularProvider.setFullResponse(true); RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) { if (typeof response.data._embedded != 'undefined') { return response.data._embedded[what]; } else { return response.data; } }); –  Chaki Jul 21 '14 at 10:25
    
Because of this Interceptor you are getting undefined when you have _embedded in your response. As you can see you are trying to fetch something from _embedded, if _embedded is there in your response. In your case 'what' will be 'users'. So your statement returns undefined. –  dhavalcengg Jul 21 '14 at 10:34

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.