Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switchMap operation only running on first call?

I have an angular application that makes a request to an Http service and calls a switchMap on another Http service. For some reason the request in the switchMap only runs the first time the parent call is called. Otherwise the parent request fires and the switchMap one doesn't, here is the code:

this._receivableService.newTenantDebitCredit(tenantCredit)
            .take(1)
            .switchMap(result =>
                // Refresh the lease receivables before giving result
                this._receivableService.getAll({
                    refresh: true,
                    where: { leaseId: this.leaseId }
                }).take(1).map(() => result)
            )
            .subscribe(
                ...
            )

How can I make the getAll request in the switch map run every time the newTenantDebitCredit method is called above it?

Edit: Here is the entirety of the function that is called on click. when i click the button the first time for a given unit both methods are executed. If I try a Unit that has already had that method called (without a refresh) only the first method is executed. I realize a lot of this may not be clear it's a rather large project at this point.

public submitTenantCredit() {
        this.isLoading = true;
        let tenantCredit: NewTenantDebitCreditData;
        let receivableDefinitions: ReceivableDefinition[] = [];

        // construct receivable defintions for NewTenantDebitData model
        receivableDefinitions = this._constructReceivableDefinitions();

        // construct data we will be POSTing to server.
        tenantCredit = new NewTenantDebitCreditData({
            siteId: this._apiConfig.siteId,
            leaseId: this.leaseId,
            isCredit: true,
            receivables: receivableDefinitions,
            reason: this.actionReason
        });

        // make service call and handle response
        this._receivableService.newTenantDebitCredit(tenantCredit)
            .take(1)
            .switchMap(result =>
                // Refresh the lease receivables before giving result
                this._receivableService.getAll({
                    refresh: true,
                    where: { leaseId: this.leaseId }
                }).take(1).map(() => result)
        )
            .take(1)
            .subscribe(
                (receivables) => {
                    this.closeReasonModal();
                    let refreshLeaseId = this.leaseId;
                    this.leaseId = refreshLeaseId;
                    this.isLoading = false;
                    this.refreshBool = !this.refreshBool;
                    this._debitCreditService.refreshUnitInfo();
                    this._notifications.success(`The tenant credit for ${this.customerName} - Unit ${this.unitNumber} was submitted successfully`);
                },
                (error) => {
                    console.error(error);
                    this.isLoading = false;
                }
            )
    }

If it helps newTenantDebitCredit() is a HTTP POST request and getAll() is a GET request.

like image 469
Zachscs Avatar asked Dec 22 '17 19:12

Zachscs


1 Answers

You used take operator. When your service observable will emit then take operator will execute first and take will chain only first emit from observable. Subsequent emit will not taken by your code. If you want to take all emits from observable then remove take from your code.

Hope it will help.

like image 166
Sandip Jaiswal Avatar answered Oct 05 '22 22:10

Sandip Jaiswal