简体   繁体   English

两个不同的 api 调用的异步可观察 - Angular 8

[英]async observable for two different api calls -Angular 8

i have a function that filters a list on add and delete and calls the respective api and binds to the async observable contact$我有一个函数可以过滤添加和删除列表,并调用相应的 api 并绑定到异步可观察的 contact$

below is my下面是我的

html

<ng-container *ngIf="contact$ | async as data; else loader">
</ng-container>
ts file

 userChanges(contactList: any[]) {

    let contactsToDel: { pubType: string, masterOutlineId: number, idsv: number, idVariant: number, fn: string, ln: string, cid: string, role: string }[] = [];
    contactList.filter(c => c.mode == 'delete').forEach(contact => {
      if (contact.masterOutlineId === this.masterOutlineId && contact.idVariant === this.idVariant && contact.procuid === this.procuid) {
    
        let idsv = this.idSVList.find(x => x.idv === contact.idVariant).idsv;
        this.idSV = idsv;
        contactsToDel.push({ pubType: this.pubType, masterOutlineId: contact.masterOutlineId, idsv: idsv, idVariant: contact.idVariant, fn: contact.fn, ln: contact.ln, cid: contact.cid, role: contact.role })
        this.contact$ = this.contactService.removeBulkContact(this.pubType, this.procuid, this.variantid, idsv, contactsToDel.filter(c => c.masterOutlineId === this.masterOutlineId && c.idVariant === this.idVariant && contact.procuid === this.procuid));

      }
    });
    let contactsToAdd: { pubType: string, masterOutlineId: number, idsv: number, idVariant: number, fn: string, ln: string, cid: string, role: string }[] = [];
    contactList.filter(c => c.mode == 'add').forEach(contact => {
      if (contact.masterOutlineId === this.masterOutlineId && contact.idVariant === this.idVariant && contact.procuid === this.procuid) {

        let idsv = this.idSVList.find(x => x.idv === contact.idVariant).idsv;
        this.idSV = idsv;
        contactsToAdd.push({ pubType: this.pubType, masterOutlineId: contact.masterOutlineId, idsv: idsv, idVariant: contact.idVariant, fn: contact.fn, ln: contact.ln, cid: contact.cid, role: contact.role })
        this.contact$ = this.contactService.saveContact(this.pubType, this.procuid, this.variantid, idsv, contactsToAdd.filter(c => c.masterOutlineId === this.masterOutlineId && c.idVariant === this.idVariant && contact.procuid === this.procuid));

      }
    });

   
  }

service file服务文件

  saveContact(pubType: any, procuid: number, variantid: number, idsv: number, contactsToAdd: any): Observable<any> {
    const body = JSON.stringify({ searchType: 'save-contact', pubType: pubType, procuid: procuid, variantid: variantid, idsv: idsv, contactsToAdd });
    console.log("body:",body);
    const endpoint = environment.endpointContacts;
    return this.http
      .post<any>(endpoint, body)
      .pipe(map(data => { return data[0] }), catchError((error) => {
        return of([{ contacts: [], proclist: [], status: error, isError: true }]);
      })
      );
  }

  removeBulkContact(pubType: any, procuid: number, variantid: number, idsv: number, contactsToDelete: any): Observable<any> {
    const body = JSON.stringify({ searchType: 'contact-remove', pubType: pubType, procuid: procuid, variantid: variantid, idsv: idsv, contactsToDelete });
    console.log("body:",body);
    const endpoint = environment.endpointContacts;
    return this.http
      .post<any>(endpoint, body)
      .pipe(map(data => { return data[0] }), catchError((error) => {
        return of([{ contacts: [], proclist: [], status: error, isError: true }]);
      })
      );
  }

issue is i am only able to either save or only delete.. if both calls are made , only save is done and not delete.问题是我只能保存或只能删除..如果两个调用都进行了,则只保存而不删除。 how do i make sure delete is completed before the save is called ?我如何确保在调用保存之前完成删除? What am i doing mistake here?我在这里做错了什么?

Have one variable and setting both calls to that this.contact$ .有一个变量并将两个调用都设置为this.contact$ So the second forEach loop is overriding the value from the previous loop.所以第二个 forEach 循环覆盖了前一个循环的值。 If you want to use async for this, you will need some refactoring.如果您想为此使用异步,则需要进行一些重构。

Can use forkJoin or combineLatest depending on how your service is set up.可以使用forkJoincombineLatest具体取决于您的服务是如何设置的。 see below.见下文。

  userChanges(contactList: any[]) {
     
    const obsArray$ = [];

    let contactsToDel: { pubType: string, masterOutlineId: number, idsv: number, idVariant: number, fn: string, ln: string, cid: string, role: string }[] = [];
    contactList.filter(c => c.mode == 'delete').forEach(contact => {
      if (contact.masterOutlineId === this.masterOutlineId && contact.idVariant === this.idVariant && contact.procuid === this.procuid) {
    
        let idsv = this.idSVList.find(x => x.idv === contact.idVariant).idsv;
        this.idSV = idsv;
        contactsToDel.push({ pubType: this.pubType, masterOutlineId: contact.masterOutlineId, idsv: idsv, idVariant: contact.idVariant, fn: contact.fn, ln: contact.ln, cid: contact.cid, role: contact.role })
        const obs$ = this.contactService.removeBulkContact(this.pubType, this.procuid, this.variantid, idsv, contactsToDel.filter(c => c.masterOutlineId === this.masterOutlineId && c.idVariant === this.idVariant && contact.procuid === this.procuid));
        obsArray$.push(obs$);
      }
    });
    let contactsToAdd: { pubType: string, masterOutlineId: number, idsv: number, idVariant: number, fn: string, ln: string, cid: string, role: string }[] = [];
    contactList.filter(c => c.mode == 'add').forEach(contact => {
      if (contact.masterOutlineId === this.masterOutlineId && contact.idVariant === this.idVariant && contact.procuid === this.procuid) {

        let idsv = this.idSVList.find(x => x.idv === contact.idVariant).idsv;
        this.idSV = idsv;
        contactsToAdd.push({ pubType: this.pubType, masterOutlineId: contact.masterOutlineId, idsv: idsv, idVariant: contact.idVariant, fn: contact.fn, ln: contact.ln, cid: contact.cid, role: contact.role })
        const obs$ = this.contactService.saveContact(this.pubType, this.procuid, this.variantid, idsv, contactsToAdd.filter(c => c.masterOutlineId === this.masterOutlineId && c.idVariant === this.idVariant && contact.procuid === this.procuid));
        obsArray$.push(obs$);
      }
    });

   this.contacts$ = combineLatest(obsArray$);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM