I'm actually working on an Angular 2 application but i have a some problems. Indeed, i have a method which get data from a database. However i'm trying to copy this data to an other one, to do that i need to do multiple HTTP request ( the requests number are never the same ).
Here is my migrate method. First I get the data from DB and then I try to post them in the other one
service.getDatas().subscribe( data => {
let datas = data.json().data;
if (datas) {
let requests = [];
for (let data of datas) {
let req = service.addData(data); // addData() return an Observable
requests.push(req);
}
let c: Observable<any> = Observable.forkJoin(requests);
return c;
}
});
Or When i'm subribing to the method i have no Response from it.
Here is my subscriber
service.migrateData(targetDB).subscribe(res => {
console.log(res);
});
I want my method to return a Response when all the data has been post ! Actually when i'm calling the addData() method it doesn't even trigger the http request, nothing happens. I tryed to use some RxJs method like concat and forkJoin but nothing. Or just i failed using them.
Here is my addData() method
addData(data) {
let headers = new Headers({
'Content-Type': 'application/json'
});
headers.append('Authorization', 'Basic ' + btoa('username + ':' + 'password));
let _data = JSON.stringify({data: data});
return this.http.post('https://something.com', _data, {headers: headers});
}
This method works very well for others use case.
Thanks for your help !