首页 文章

无法使用forkJoin创建2个独立的Observable

提问于
浏览
0

我有一个函数,它将ID作为参数并进行HTTP调用,具体取决于ID . 它返回一个Observable . 我希望不同参数的调用完全独立,但它们相互影响 .

我用
RxJS 5.0.0-beta.2
Angular 2.0.0-beta.7用于http调用
打字稿1.8.2

Code:

getCharacterDetails (id : number) : Observable<CharacterDetails> {
    let keys : SpreadsheetKeys = this.characters[id];
    if (!keys) {
        return null;
    }

    let frontUrl : string = `${BASE_URL}/${CELLS}/${keys.spreadsheetKey}/${keys.frontWorksheetKey}/${OPTIONS}`;
    let backUrl : string = `${BASE_URL}/${CELLS}/${keys.spreadsheetKey}/${keys.backWorksheetKey}/${OPTIONS}`;

    return Observable.forkJoin(

        this.http.get(frontUrl).map(response => response.json()),
        this.http.get(backUrl).map(response => response.json()))

        .map((response : any[]) => {

            let character : CharacterDetails = EMPTY_MODEL;

            console.log(JSON.stringify(character)); // The 1st console.log

            applyFrontSheetToCharacter(response[0], character);
            applyBackSheetToCharacter(response[1], character);

            console.log(JSON.stringify(character)); // The 2nd console.log

            return character;
        });
}

Expected behavior:

我打电话
getCharacterDetails(1).subscribe((details) => { this.details = details }));
第一个console.log打印我的EMPTY_MODEL .
第二个console.log打印ID 1的字符模型 .

然后我打电话
getCharacterDetails(2).subscribe((details) => { this.details = details });
第一个console.log打印我的EMPTY_MODEL .
第二个console.log打印ID 2的字符模型 .

Actual behavior:

我打电话
getCharacterDetails(1).subscribe((details) => { this.details = details }));
第一个console.log打印我的EMPTY_MODEL .
第二个console.log打印ID 1的字符模型 .

然后我打电话给 getCharacterDetails(2).subscribe((details) => { this.details = details });
The 1st console.log prints the character model for ID 1. < - 问题
第二个console.log打印ID 2的字符模型 .

Why are the 2 calls not completely independent? How does the 2nd call even know about the data from the first call?

1 回答

  • 1

    问题很简单,我完全滥用了EMPTY_MODEL . 它基本上是我在.map()函数中编辑的类范围变量 . 一个与RxJS,Angular或HTTP无关的愚蠢错误

相关问题