簡體   English   中英

rxjs可觀察的合並未按預期工作

[英]rxjs Observable merge doesn't work as expected

我需要為POST請求生成以下結果:

{
"names": [
    {
        "id": "t3xcb9xAyX",
        "username": "Gennaro"
    },
    {
        "id": "Csdu65RKon",
        "username": "Marco"
    },
    ...
],
"createdAt":"04/07/2018 - 11.49.51"
}

因此,我使用rxjs完成了這項工作:我創建了兩個Observable(一個用於名稱,一個用於createdAt)並在最后合並:

const notObj = utils.getNotificationType(codeProduct, Parse);
const csvObj = utils.getNotificationType(codeProduct, Parse);

const query = new Parse.Query(notObj);
const dateQuery = new Parse.Query(csvObj).descending('createdAt');


const names = from(query.find())
    .map(el => el.map((e) => {
        return {
            id: e.id,
            username: e.get('username')
        }
    }))
    .mergeMap((arr) => Observable.of({
        names: arr
    }));

const lastUpdate = from(dateQuery.first())
    .map(res => moment(res.createdAt).format('DD/MM/YYYY - HH:mm:ss'))
    .map(res => {
        return {
            createdAt: res
        }
    });


merge(names, lastUpdate)
    .subscribe(
        (data) => res.send(serialize(data)),
        (error) => res.send(serialize(error)),
        () => console.log('complete')
    );

問題是,最終合並只能檢索我"names" 使用.zip()運算符可以得到另一個結果,但是我有一個JSON數組而不是一個對象。

我的問題是:為什么merge()不合並兩個結果,而只合並第一個? 謝謝

那不是merge作用。 它合並可觀察流而不是對象本身。 使用forkJoin代替,它將發出結果數組,然后將其自己與map合並:

const names$ = ...;
const lastUpdate$ = ...;

forkJoin(names$, lastUpdate$)
  .map(([ names, lastUpdate ]) => ({ names, lastUpdate }))
  .subscribe(...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM