Home > Software design >  Angular HttpClient is not executing
Angular HttpClient is not executing

Time:06-17

So I have the following function. It is executed until the first console.log, but not further. I checked network traffic, and the API call is never executed, but i don't know why.

    deleteReward(id: number): Observable<boolean> {

        const httpOptions = {
            headers: new HttpHeaders({
                'Content-Type': 'application/json',
                'Authorization': 'Bearer '   localStorage.getItem('accessToken'),
            })
        };

        console.log('here in delete');

        return this.rewards$.pipe(
            take(1),
            switchMap(rewards => this._httpClient.post(this._url   'rewards/delete', { rewardId: id }, httpOptions).pipe(
                map((res) => {

                    console.log(res);
                    console.log('here in delete 2');

                    // Find the index of the deleted reward
                    const index = rewards.findIndex(item => item.id === id);

                    // Delete the reward
                    rewards.splice(index, 1);

                    // Update the rewards
                    this._rewards.next(rewards);

                    // Return the deleted status
                    return true;
                })
            ))
        );
    }

Has anyone any idea why this function is not working? I am using a similar function for updating and it is working flawlessly, But the delete function is not.

I am a beginner with angular, maybe there is a simple solution for this.

CodePudding user response:

I looks like you just forget to subscribe the observable: deleteReward(1) - does not work, deleteReward(1).subscribe() - should work

  • Related