[英]Random results from assynchronous queries on REST-api
I created following controller with 3 differents $http GET calls to a rest-api. 我创建了以下控制器,其中包含3个不同的$ http GET调用rest-api。
$http({method: 'GET', url: REST_CONFIG.url+'/api/lims/data/runs/list'})
.success(function(data, status, headers, config) {
form.runs = data;
})
.error(function(data, status, headers, config) {
form.runs = [];
});
form.data.analysis = {"analysisName": "","analysisprofile": {"workflows": []},"run": ""};
//Get all Default Workflows
$http({method: 'GET', url: REST_CONFIG.url+'/api/workflows/default/list'})
.success(function(data, status, headers, config) {
form.workflows = data;
})
.error(function(data, status, headers, config) {
form.workflows = [];
});
//Get all databases
$http({method: 'GET', url: REST_CONFIG.url+'/api/list-databases'})
.success(function(data, status, headers, config) {
form.databases = data;
})
.error(function(data, status, headers, config) {
form.databases = [];
});
Sometimes I have the same results from query1 and query2 (query2 have the result from query1). 有时我从query1和query2得到相同的结果(query2有query1的结果)。 In that case the rest-api do 2 times the query1.
在这种情况下,rest-api执行query1的2次。 My browser say that the http queries are good (3 differents url).
我的浏览器说http查询很好(3个不同的url)。 This is weird and really annoying.
这很奇怪,真的很烦人。 I also tried to do:
我也尝试过这样做:
//Get all runs
runs = $http({method: 'GET', url: REST_CONFIG.url+'/api/lims/data/runs/list'});
//Get all Default workflows
defaultWorkflows = $http({method: 'GET', url: REST_CONFIG.url+'/api/workflows/default/list'});
//Get all databases
databases = $http({method: 'GET', url: REST_CONFIG.url+'/api/list-databases'});
$q.all([runs, defaultWorkflows, databases]).then(function(values) {
form.runs = values[0].data;
form.workflows = values[1].data;
form.databases = values[2].data;
});
Nothing worked. 没有任何效果。 Is it coming from the rest-api?
它来自rest-api吗? Or I am doing something wrong?
或者我做错了什么?
EDIT Problem solved. 编辑问题解决了。 The key point was the use of $q with promise and deffer().
关键点是使用$ q with promise和deffer()。 This plunkr helped me a lot: http://plnkr.co/edit/NGMp4ycmaCqVOmgohN53?p=preview
这个plunkr给了我很多帮助: http ://plnkr.co/edit/NGMp4ycmaCqVOmgohN53?p=preview
I use the following code: 我使用以下代码:
var getInfo = function(){
var promises = [];
var urls = [];
urls.push(REST_CONFIG.url+'/api/lims/data/runs/list');
urls.push(REST_CONFIG.url+'/api/workflows/default/list');
urls.push(REST_CONFIG.url+'/api/list-databases');
angular.forEach(urls, function(url){
var deffered = $q.defer();
$http({method: 'GET', url: url})
.then(function successCallback(response) {
deffered.resolve(response.data);
}, function errorCallback(response) {
deffered.reject();
});
promises.push(deffered.promise);
})
return $q.all(promises);
}
var init = function(){
var promiseInfo = getInfo();
promiseInfo.then(function(datas){
form.runs = datas[0];
form.workflows = datas[1];
form.databases = datas[2];
})
};
You need to be sure to wait for all of the requests to finish before using forms
, for instance, by using $q.all
. 在使用
forms
之前,您需要确保等待所有请求完成,例如,使用$q.all
。 Also, be sure to use then
and catch
; 另外,一定要使用
then
catch
; success
and error
were deprecated back in v1.5: success
和error
在v1.5中被弃用:
function getInfo() {
// Assuming `form` exists here...
var promises = [];
promises.push(
$http({method: 'GET', url: REST_CONFIG.url+'/api/lims/data/runs/list'})
.then(function(data, status, headers, config) {
form.runs = data;
})
.catch(function(data, status, headers, config) {
form.runs = [];
})
);
form.data.analysis = {"analysisName": "","analysisprofile": {"workflows": []},"run": ""};
//Get all Default Workflows
promises.push(
$http({method: 'GET', url: REST_CONFIG.url+'/api/workflows/default/list'})
.then(function(data, status, headers, config) {
form.workflows = data;
})
.catch(function(data, status, headers, config) {
form.workflows = [];
})
);
//Get all databases
promises.push(
$http({method: 'GET', url: REST_CONFIG.url+'/api/list-databases'})
.then(function(data, status, headers, config) {
form.databases = data;
})
.catch(function(data, status, headers, config) {
form.databases = [];
})
);
return $q.all(promises).then(() => form);
}
That returns a promise that will resolve to form
when all three of the requests are complete (it won't reject, because rejections are turned into resolutions via the catch
handlers, in keeping with your original code). 返回将解析承诺
form
时,所有三个要求的完成(它不会拒绝,因为拒绝被通过变成分辨率catch
处理程序,在您的原始代码保持)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.