0

Out of a factory I get data using express and MongoDB with mongoose defined on a model, but I can get the document by the id and have it on the scope but I need to iterate through the array of crews, it works with ng-repeater on the view but I want to pick one subDocument or the array $scope.contractor.crews to send it to a view on from the controller, as the output below shows it is on the $scope.

Here is the Service using $resource

  .factory('Contractors', ['$resource', function($resource){
    return $resource('/contractors/:id', null, {
   'update': { method:'PUT' }
  });
}])

Here is my call to the factory. $scope.contractor = Contractors.get({id: $routeParams.coid });

contractor: f
$promise: Object
$resolved: true
__v: 0
_id: "553569cbbb73900f82b8359a"
active: true
address: "In Delano too"
commission: 30
crews: Array[4]
0: Object
1: Object
2: null
3: null
length: 4
__proto__: Array[0]
fax: "6612953423"
name: "Magbar Contracting"
note: "This one has Mayra maya"
phone: "6613458584"
updated_at: "2015-04-20T21:04:11.681Z"

I have tried with

            $scope.crews = function() {
            var crews = [];
            angular.forEach($scope.contractor.crews, function(crew) {
            crews.push(crew)
            });
            return crews;
        };

        console.log($scope.crews());

but all I get is []

Thanks.

1
  • Sorry the typo is fix is on the post the code is right but don't work Commented May 2, 2015 at 23:52

1 Answer 1

1

Change

$scope.contractor = Contractors.get({id: $routeParams.coid });

To

Contractors.get({id: $routeParams.coid }).then(function(response) {
 $scope.contractor = response;
});

because get returns a Promise object and you get the resolved value from the then callback.

Sign up to request clarification or add additional context in comments.

3 Comments

The typo was on the post only, I am being for hours dealing with this but not work.
Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted.)
Your answer got me to the right path, I only have to add $promise before .them to make it work with $resource - Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.