Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json AngularJS+;解析RESTAPI-分页1000多个结果_Json_Angularjs_Rest_Parse Platform - Fatal编程技术网

Json AngularJS+;解析RESTAPI-分页1000多个结果

Json AngularJS+;解析RESTAPI-分页1000多个结果,json,angularjs,rest,parse-platform,Json,Angularjs,Rest,Parse Platform,我使用parserestapi+AngularJS,并且我试图在每个查询中获得1000多个项目。我尝试开发一个递归函数并连接每个查询,直到获得所有数据。我的问题是我无法成功连接JSON对象。以下是我所拥有的: $scope.getAllItems = function(queryLimit, querySkip, query) { $http({method : 'GET', url : 'https://api.parse.com/1/class

我使用parserestapi+AngularJS,并且我试图在每个查询中获得1000多个项目。我尝试开发一个递归函数并连接每个查询,直到获得所有数据。我的问题是我无法成功连接JSON对象。以下是我所拥有的:

    $scope.getAllItems = function(queryLimit, querySkip, query) {
        $http({method : 'GET',
            url : 'https://api.parse.com/1/classes/myClass', 
            headers: { 'X-Parse-Application-Id':'XXX','X-Parse-REST-API-Key':'YYY'},
            params: {limit:queryLimit, skip:querySkip},

        }).success(function(data, status) {
                query.concat(data.results); 
                if(query.lenth == queryLimit) {
                    querySkip += queryLimit;
                    queryLimit += 100;
                    $scope.getAllItems(queryLimit, querySkip, query);
                } else {
                    $scope.clients = query;
                }

            })
            .error(function(data, status) {
                alert("Error");
            });
    };
    var myQuery = angular.toJson([]); //Am I creating an empty JSON Obj?
    $scope.getAllItems(100,0, myQuery);

是否有更好的解决方案来实现这一点?

可能有更好、更简洁的想法,但这是我为自己制定的

为我效劳

fetch : function(page, perpage) {
    var query = // build the query
    // the whole answer to your question might be this line:
    query.limit(perpage).skip(page*perpage);
    return query.find();
},

fetchCount : function() {
    var query = // build the same query as above
    return query.count();
},
在控制器中

$scope.page = 0;       // the page we're on
$scope.perpage = 30;   // objects per page

MyService.fetchCount().then(function(count) {
    var pagesCount = Math.ceil(count / $scope.perpage);
    $scope.pages = [];
    // pages is just an array of ints to give the view page number buttons
    for (var i=0; i<pagesCount; i++) { $scope.pages.push(i); }
    fetch();
});

function fetch() {
    return MyService.fetch($scope.page, $scope.perpage)).then(function(results) {
        $scope.results = results;
    });
}

// functions to do page navigation
$scope.nextPage = function() {
    $scope.page += 1;
    fetch();
};

$scope.prevPage = function() {
    $scope.page -= 1;
    fetch();
};

$scope.selectedPage = function(p) {
    $scope.page = p;
    fetch();
};
$scope.page=0;//我们现在的页面
$scope.perpage=30;//每页对象数
MyService.fetchCount().then(函数(计数){
var pagescont=Math.ceil(count/$scope.perpage);
$scope.pages=[];
//pages只是一个int数组,用于提供查看页码按钮
对于(变量i=0;i»


我修复了递归函数,现在它可以工作了。它是:

$scope.getAllItems = function(queryLimit, querySkip, query, first) {

        $http({method : 'GET',
            url : 'https://api.parse.com/1/classes/myClass', 
            headers: { 'X-Parse-Application-Id':'XXX','X-Parse-REST-API-Key':'YYY'},
            params: {limit:queryLimit, skip:querySkip},

        }).success(function(data, status) {
                if(first) {
                    query = data.results;
                    first = !first;
                    if(query.length == queryLimit) {
                        querySkip += queryLimit;
                        $scope.getAllItems(queryLimit, querySkip, query, first);
                    } else {
                        $scope.clients = query;
                    }
                } else {
                    var newQ = data.results;
                    for (var i = 0 ; i < newQ.length ; i++) {
                      query.push(newQ[i]);
                    }
                    if(query.length == queryLimit + querySkip) {
                        querySkip += queryLimit;
                        $scope.getAllItems(queryLimit, querySkip, query, first);
                    } else {
                        $scope.clients = query;
                    }
                }
            })
            .error(function(data, status) {
                alert("Error");
            });
    };
$scope.getAllItems=函数(queryLimit,querySkip,query,first){
$http({method:'GET',
网址:'https://api.parse.com/1/classes/myClass', 
标题:{'X-Parse-Application-Id':'XXX','X-Parse-REST-API-Key':'YYY'},
参数:{limit:queryLimit,skip:querySkip},
}).成功(功能(数据、状态){
如果(第一){
查询=数据。结果;
第一个=!第一个;
if(query.length==queryLimit){
querySkip+=queryLimit;
$scope.getAllItems(queryLimit、querySkip、query、first);
}否则{
$scope.clients=查询;
}
}否则{
var newQ=数据。结果;
对于(变量i=0;i

简单地将每个元素推到我的空数组中,我也在变异
queryLimit
而不是
querySkip
,以便迭代所有元素。

每个服务器响应都是一个完整的JSON字符串或不完整的JSON字符串,必须在多个查询中下载?如果每个服务器响应都是某个对象的JSON编码数组对象,然后您可以创建空数组,并为每个成功的请求将下载的项目添加到此数组中。我的理解是,这是一个完整的JSON字符串,解析API不会让您每次查询获得超过1000个项目,因此基本上我将获得前1000个项目。好吧。只需创建数组并在每个请求上添加项目;-)您正在为myQuery植入一个字符串-我想您只需要var myQuery=[];感谢您分享您的解决方案。您好。我可以知道angularjs是否能够毫无问题地解析1000多条记录吗?
$scope.getAllItems = function(queryLimit, querySkip, query, first) {

        $http({method : 'GET',
            url : 'https://api.parse.com/1/classes/myClass', 
            headers: { 'X-Parse-Application-Id':'XXX','X-Parse-REST-API-Key':'YYY'},
            params: {limit:queryLimit, skip:querySkip},

        }).success(function(data, status) {
                if(first) {
                    query = data.results;
                    first = !first;
                    if(query.length == queryLimit) {
                        querySkip += queryLimit;
                        $scope.getAllItems(queryLimit, querySkip, query, first);
                    } else {
                        $scope.clients = query;
                    }
                } else {
                    var newQ = data.results;
                    for (var i = 0 ; i < newQ.length ; i++) {
                      query.push(newQ[i]);
                    }
                    if(query.length == queryLimit + querySkip) {
                        querySkip += queryLimit;
                        $scope.getAllItems(queryLimit, querySkip, query, first);
                    } else {
                        $scope.clients = query;
                    }
                }
            })
            .error(function(data, status) {
                alert("Error");
            });
    };