Ioan Adrian Duliba Ioan Adrian Duliba - 5 months ago 10
AngularJS Question

How I get the user and atach to an object javacript

$http.get("api/location/getReviews/" + $routeParams.id)
.success(function (data) {
$scope.reviews = {};
$scope.reviews = data;
angular.forEach($scope.reviews,
function (value, index) {
$http.get("api/user/" + value.UserId)
.success(function (user) {
$scope.value.User = user;
});
});
});


I have the following code and what I am trying to do is to get user
from
userId
and attach to a review and send it into view because my review has a
userId
, and I need user email. when I list the reviews I want to show the email not the id but I don't know how. It gives me cannot read property or undefined User.

Anyone can explain what happen?

Answer

You can place the user object returned from the API call in the loop into the indexed review object in the reviews Array this way :

    angular.forEach($scope.reviews, function (value, index) {

            $http.get("api/user/" + value.UserId)
            .success(function (user) {

                  $scope.reviews[index].User = user;

            });

   });