Reacting
Reacting

Reputation: 6123

Error performing get request with Angular $resource

I am doing a function in order to do a get request to a specified route which sends a string to the backend in order to return some results based on that string, and I am getting this error:

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object

Here is the Angular part:

View:

<input type="text" data-ng-model="searchStr">
<textarea> {{responseData}} </textarea>

Ctrl:

$scope.$watch('searchStr', function (tmpStr)
{
  if (!tmpStr || tmpStr.length == 0)
    return 0;


    // if searchStr is still the same..
    // go ahead and retrieve the data
    if (tmpStr === $scope.searchStr) {   
      Search.get({ 'search': $scope.searchStr })
      .$promise.then(function(data) {
        $scope.responseData = data;
      })
    }
});

Service:

angular.module('MyApp')
  .factory('Search', function($resource) {
    return $resource('/api/search/:search', {});
  });

And this is what I have in the node part:

app.get('/api/search/:search', function(req, res, next) {
  request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + req.params.search, function (error, response, body) {
    console.log(error, response, body);
    res.end(body);
  });
});

I know that in the backend everything is right because if I do a post from postman to http://localhost:3000/api/search/all where all is the supposed string/search-term sent from the frontend to that route it is returning all of the results I need. So it seems to be something that I am doing something wrong in the frontend.

Any suggestions?

Upvotes: 1

Views: 60

Answers (1)

Thibs
Thibs

Reputation: 8258

I think you need to specify isArray:false on your service.

return $resource('/api/search/all', {}, {
        'query' : {method : 'GET', isArray : false},
    });

Upvotes: 1

Related Questions