Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Angular 角度5中的并行请求_Angular_Typescript_Angular5 - Fatal编程技术网

Angular 角度5中的并行请求

Angular 角度5中的并行请求,angular,typescript,angular5,Angular,Typescript,Angular5,我试图提出并行请求,获取并合并结果。 为此,我使用以下功能: getStudent(query): Observable<any> { const code = this.http.get( `http://localhost:8090/etudiantFiltre?codEtudiant=${query}` ); const prenom = this.http.get( `http://localhost:8090/

我试图提出并行请求,获取并合并结果。 为此,我使用以下功能:

    getStudent(query): Observable<any> {
    const code = this.http.get(
        `http://localhost:8090/etudiantFiltre?codEtudiant=${query}`
    );
    const prenom = this.http.get(
        `http://localhost:8090/etudiantFiltre?prenom1=${query}`
    );
    const nom = this.http.get(
        `http://localhost:8090/etudiantFiltre?patronyme=${query}`
    );
    return Observable.forkJoin([code, nom, prenom]).map(responses => {
        console.log(`code : ${code}
                     nom : ${nom}
                     prenom : ${prenom}`);
        return [].concat(...responses);
    });
}
但是,当我试图打印该方法的结果时,以下是我得到的结果,我真的不明白为什么:

{
    "_isScalar": false,
    "source": {
    "_isScalar": false,
    "sources": [
        {
        "_isScalar": false,
        "source": {
            "_isScalar": false,
            "source": {
            "_isScalar": false,
            "source": {
                "_isScalar": true,
                "value": {
                "url": "http://localhost:8090/etudiantFiltre?codEtudiant=firstname",
                "body": null,
                "reportProgress": false,
                "withCredentials": false,
                "responseType": "json",
                "method": "GET",
                "headers": {
                    "normalizedNames": {},
                    "lazyUpdate": null,
                    "headers": {}
                    ...

如果需要并行请求,也可以尝试使用
forkJoin
studentObservables。此外,您还可以使用
map
而不是
forEach
来获得更清晰的代码

getAllStudent(query) {
    let studentObservables = query.split(' ')
        .map(element => this.getStudent(element)));
    return Observable.forkJoin(studentObservable);
}

ngOnInit() {
    this.getAllStudent('name firstname')
        .subscribe(data => console.log(data));
}

您可以继续使用forkJoin并使用
combinelateest
将所有请求统一在一起。如果你想为你的最终观察得到一个简单的答案。您可以使用
mergeMap
谁将接收
Array
并可以返回简单的
Response[]

const keywords = ['yanis','git'];
const request$ = [];

keywords.forEach((keyword) => {
        //Create array of ForkJoin Observable.
        request$.push(forkJoin([
            this.http.get('https://randomuser.me/api/?query='+keyword),
            this.http.get('https://randomuser.me/api/?query='+keyword),
            this.http.get('https://randomuser.me/api/?query='+keyword)
        ]));
});
// We combine all together and provide mergeMap strategy.
// Possible Observable here : combineLatest, forkJoin, combineAll ...
Observable.combineLatest(request$).mergeMap(e => {
    //All is merged to returned array. Here you can also deduplicate result.
    const returned = [];
    e.forEach(t => {
        t.forEach(i => returned.push(i));
    });
    return returned;

}).subscribe(e => {
    //You will receive each result returned by mergeMap.
    console.log(e);
});

在线示例:

您好,您能为每种请求提供json输出示例吗。实际上,我认为你的可观察对象试图递归地将每个响应对象合并在一起。为了防止这种情况,请尝试使用“Observable.merge([code,nom,prenom])”,然后对于“统一”部分,我将在询问json输出时为您提供实现。如果您询问三个请求的结果,它们都具有相同的json体。至于结果,这就是我使用它时得到的结果,
getAllStudent(query) {
    let studentObservables = query.split(' ')
        .map(element => this.getStudent(element)));
    return Observable.forkJoin(studentObservable);
}

ngOnInit() {
    this.getAllStudent('name firstname')
        .subscribe(data => console.log(data));
}
const keywords = ['yanis','git'];
const request$ = [];

keywords.forEach((keyword) => {
        //Create array of ForkJoin Observable.
        request$.push(forkJoin([
            this.http.get('https://randomuser.me/api/?query='+keyword),
            this.http.get('https://randomuser.me/api/?query='+keyword),
            this.http.get('https://randomuser.me/api/?query='+keyword)
        ]));
});
// We combine all together and provide mergeMap strategy.
// Possible Observable here : combineLatest, forkJoin, combineAll ...
Observable.combineLatest(request$).mergeMap(e => {
    //All is merged to returned array. Here you can also deduplicate result.
    const returned = [];
    e.forEach(t => {
        t.forEach(i => returned.push(i));
    });
    return returned;

}).subscribe(e => {
    //You will receive each result returned by mergeMap.
    console.log(e);
});