0

I'm using an Angular HTTP Resource to get some data from an API. The method on the API is actually a PUT, but returns an array of data. (Note, it's not my API)

It performs the HTTP Call perfectly and I can see in the Network on Google Developer Tools my response is right. However, I keep getting "TypeError: undefined is not a function" when trying to work with the response data.

Here are some of the different methods I've tried and each one gives the same TypeError:

HTTP Resource

.factory('Sports', ['$resource', 'SPORTS_CONFIG',
    function($resource, SPORTS_CONFIG) {
        return $resource(SPORTS_CONFIG.URL, {}, {
            get: {
                method: 'PUT', 
                isArray: true 
            }
        });
    }
])

Angular JS Attempts

// Construct Sports Body
var s = new Sports($scope.sport);

// Attempt 1
s.$get({}, function(data){

});

// Attempt 2
s.$get({}).then(function(data){

});

// Attempt 3
s.$get({}).$promise.then(data){

});

EDIT

After looking at my error code more, it looks like the error is occurring on 'isArray' in my HTTP Resource. When I set isArray to false, it errors because it expects an object but gets an array. When I keep it true, it errors and says TypeError: undefined is not a function. When I click the javascript line it's 'isArray'.

SECOND EDIT

I've gotten it to work using the following code:

$scope.teams = Sports.get($scope.sport);

However, $scope.teams = [0, $promise, $resolved] . How can I only get [0] instead of the $promise and $resolved?

7
  • 1
    Have you tried s.$get().success(function(data, status, headers, config) { }); ? Commented Feb 3, 2015 at 14:38
  • I've added what s is, the $http resource, and an update to the error location. Could you please see the edits. Commented Feb 3, 2015 at 15:07
  • Can you tell us, what PUT response you are getting? Commented Feb 3, 2015 at 15:23
  • Status: 200 OK Response: [{"name": Football, "id": 2}] Commented Feb 3, 2015 at 15:27
  • I assume the response is [{"name": "Football", "id": "2"}] (mind the quotes). If its not then the response is faulty. Do validation with JSON Lint Commented Feb 3, 2015 at 16:27

4 Answers 4

0

the argument to your 'then' function should itself be a function.

eg:

s.$get({}).then(function (data){

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

1 Comment

I've added what s is, the $http resource, and an update to the error location. Could you please see the edits.
0

Your syntax looks wrong, follow this approach instead:

$http.get('/someurl').then(function(response) {
        //do something with response.data;
      }, function(errResponse) {
        console.error('Error while fetching data');
      });

As you can see you should be passing in a function which passes in the response from the server, and then you can get at your data.

1 Comment

I've added what s is, the $http resource, and an update to the error location. Could you please see the edits.
0

You can try something like this (with a function)

$http({
        method : "GET",
        url : "your_url"
    }).success(function(data, status, headers, config) {
        $scope.yourVariable= data;
    }).error(function(data, status, headers, config) {
        alert("Request failed. HTTP status:" + status);
    });

1 Comment

I've added what s is, the $http resource, and an update to the error location. Could you please see the edits.
0

Due to the edit above, this answer became outdated.

As I cannot comment right now, the way to get the just first item as you'd like is simple: $scope.teams = Sports.get($scope.sport)[0];

1 Comment

I've added what s is, the $http resource, and an update to the error location. Could you please see the edits.

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.