使用Angular $ resource执行获取请求时出错

反应

我正在执行一个函数,以便向get指定的路由发出请求,该路由将字符串发送到后端,以便基于该字符串返回一些结果,并且出现此错误:

错误:[$ resource:badcfg]资源配置错误。预期的响应包含一个数组但有一个对象

这是角度部分:

看法:

<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;
      })
    }
});

服务:

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

这就是我在节点部分中所拥有的:

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);
  });
});

我知道在后端一切都是正确的,因为如果我在邮递员http://localhost:3000/api/search/all那里发布帖子,all那么从前端发送到该路由的假定字符串/搜索词在哪里,它将返回我需要的所有结果。所以看来我在前端做错了什么。

有什么建议么?

锡伯斯

我认为您需要在服务上指定isArray:false。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章