forkJoin可以用一个Observable中的值覆盖另一个的值吗?

时间:2018-01-09 14:57:50

标签: angular http rxjs observable fork-join

我是Angular 2的新手,我希望我能解释这个。我有3个http路径 - 1,2和& 3.我想合并/覆盖这些。我希望path3成为我的默认设置。

如果path2中存在一个值,我希望此值覆盖path3中的该值,并且与path1相同。因此,path1中的值是最重要的,但如果path1中的值为null,则应使用path2中的值,然后使用path3。我尝试使用forkJoin但没有成功。

如果path1或path2中的值不存在,则显示路径名而不是指定的值。这就是我所拥有的:

export class Loader1 implements Loader2 {
    constructor(private http: HttpClient) {
    }
    public getMethod(lang: string): any {
        return Observable.forkjoin(
            this.http.get(path1 + '.json').map((res) => res)
                .catch((res) => Observable.of(null)),
            this.http.get(path2 + '.json').map((res) => res),
            this.http.get(path3 + '.json').map((res) => res)
        ).map(results => {
            let emptyCheck = this.http.get(path1 + '.json');
            if (emptyCheck.catch((res) => Observable.of(null))) {
                results[0] = results[1];
            } else {
                emptyCheck.map((res) => res);
            }
            let result = Object.assign({}, results[2], results[1], results[0]);
            return result;
        });
    }
}

path1:
{
        "value1": "First number1",
        "value5": "First number5",
        "value10": "First number10"
}

path2:
{
        "value1": "Second number1",
        "value2": "Second number2",
        "value4": "Second number4",
        "value5": "Second number5",
        "value6": "Second number6",
        "value7": "Second number7",
        "value10": "Second number10"
}

path3:
{
        "value1": "Third number1",
        "value2": "Third number2",
        "value3": "Third number3",
        "value4": "Third number4",
        "value5": "Third number5",
        "value6": "Third number6",
        "value7": "Third number7",
        "value8": "Third number8",
        "value9": "Third number9",
        "value10": "Third number10"
}

Expected Output:
        First number1
        Second number2
        Third number3
        Second number4
        First number5
        Second number6
        Second number7
        Third number8
        Third number9
        First number10

Actual output:
        path1.value1
        path2.value2
        Third number3
        path2.value4
        path1.value5
        path2.value6
        path2.value7
        Third number8
        Third number9
        path1.value10

2 个答案:

答案 0 :(得分:0)

我创建了一个示例jsbin ..它正在按预期工作...请查看并分享您的反馈。您也可以使用它来复制您的问题。

    const path1 = {
            "value1": "First number1",
            "value5": "First number5",
            "value10": "First number10"
    };

    const path2 = {
            "value1": "Second number1",
            "value2": "Second number2",
            "value4": "Second number4",
            "value5": "Second number5",
            "value6": "Second number6",
            "value7": "Second number7",
            "value10": "Second number10"
    };

    const path3 = {
            "value1": "Third number1",
            "value2": "Third number2",
            "value3": "Third number3",
            "value4": "Third number4",
            "value5": "Third number5",
            "value6": "Third number6",
            "value7": "Third number7",
            "value8": "Third number8",
            "value9": "Third number9",
            "value10": "Third number10"
    }

    const source = Rx.Observable.forkJoin(
    Rx.Observable.of(path1),
    Rx.Observable.of(path2),
    Rx.Observable.of(path3)
    ).map( result => Object.assign({}, result[2], result[1], result[0]))

    const subscription = source.subscribe(val => console.log('First',val));

    let example1 = Object.assign({}, path3, path2, path1);
    console.log('second', example1)

    let example2 = Object.assign({}, null, null, path1);
    console.log('Third', example2)

JSBIN Sample

答案 1 :(得分:0)

这对我有用。这可能是因为路径名很长。

1) `python ./manage.py test appname.tests_v1.classname.function_name`

2) Else 

`pip install nose pinocchio django_nose`

add below line in settings.py 

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['--with-spec', '--spec-color']  

then run 

`python manage.py test`