AngularJS - How to construct the url of endpoint with 2 parameters

73 Views Asked by At

I am relatively new to AngularJS and I was working on making the following within the Service with factory.

I have the following call within the Service:

factory.getUsers = function () {
    var USER_ENDPOINT = '/api/user'
    var deferred = $q.defer();

    $http.get(USER_ENDPOINT).then(
        function (success) {
           var users = success.data.map(function (c) { return { UsertId: c.UserId, UserName: c.UserName }; });
    deferred.resolve(users);
}
    , function (failure) {
        deferred.reject(failure);
      }
);

   return deferred.promise;
};

factory.getLocations = function (userId) {
        var LOCATION_ENDPOINT = '/api/user/' + userId + '/location'
        var deferred = $q.defer();

        $http.get(LOCATION_ENDPOINT).then(
            function (success) {

                var locations = success.data.map(function (c) { return { LocationId: c.LocationId, LocationName: c.LocationName }; });

                deferred.resolve(locations);
            }
            , function (failure) {
                deferred.reject(failure);
            }
        );

        return deferred.promise;
    };

Here is my problem. Calling userId and locationId within the below Endpoint is providing me back the userId but when it reaches locationId is returning 'undefined'. When I call the exact path within the browser it returns the proper data based on this path but not within the below approach.

factory.getService = function (userId, locationId) {
        var SERVICE_ENDPOINT = '/api/user/' + userId + '/location/' + locationId;
        var deferred = $q.defer();

        $http.get(SERVICE_ENDPOINT).then(
            function (success) {

                var services = success.data.map(function (c) { return { ServiceId: c.ServiceId, ServiceName: c.ServiceName }; });

                deferred.resolve(services);
            }
            , function (failure) {
                deferred.reject(failure);
            }
        );

        return deferred.promise;
    };

If I replace the userId and place the locationId first that returns the locationId but this time clientId is undefined.

I would appreciate help on this one. Thank you in advance!

0

There are 0 best solutions below