0

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.

1
  • 1
    Are you subscribing to the observable somewhere in your code? Commented Jun 16, 2022 at 23:51

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Damn that was too easy. I was sitting here for days breaking my head about it. Thank you very much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.