Tom Söderlund
Tom Söderlund

Reputation: 4747

Can't access properties from a ngResource object

I fetch a database object with the following code:

// Lookup Project object from server
$scope.project = projectModel.get({}, {'id': session.projectId});
console.log('projectModel', $scope.project);

The output of the above is:

projectModel

I can't really interpret that output - i.e. what are the variables outside the {...}? In any case, trying to read e.g. projectModel.name just returns undefined - why?

Upvotes: 1

Views: 811

Answers (3)

kubuntu
kubuntu

Reputation: 2535

You made an async call and the value you want is most likely not yet assigned when you tried to print to console. You could assign $scope.project this way

projectModel.get({'id': session.projectId}, 
    function (data) {
        $scope.project = data;
});

Upvotes: 2

rajmohan
rajmohan

Reputation: 1618

projectModel.get({}, { 'id': session.projectId },
    function (successResponse) {
        // success callback
        console.log('successResponse:', successResponse);
    },
    function (errorResponse) {
        // failure callback
        console.log(errorResponse);
    }
);

try this. You will get the promise value in success response.

Upvotes: 1

licancabur
licancabur

Reputation: 645

projectModel is not the same as $scope.project, in $scope.project you get a resolved promise and when it is resolved, you can access name with: console.log('$scope.project.name'); Your resource - projectModel is propably a deffered object

You can read about promises here: $q

Upvotes: 0

Related Questions