Search code examples
angularrxjsrecaptcharxjs-observables

Unhandled Promise rejection: You provided 'undefined' where a stream was expected. Using SwitchMap and a function of type Observable


this is my first time using switchMap from the rxjs library.

Im guessing this error message means its unhappy with how I am doing things.

Observe my addemail function

addemail(form:NgForm){
    this.googlerecaptchaservice.verifyinteraction('general_marketing_email_signup')
      .subscribe(
        (score: any)=>{
          console.log(score);
          const value = form.value.emailaddform;
          this.addemailservice.add_email_to_general_marketing(value)
            .subscribe(
              (req: any)=>{
                console.log('here is the test');
                console.log(req);
              }
            );
        });

  }

more specifically the googlerecaptchaservice.verifyinteraction

verifyinteraction(action): Observable<any>{
    return this.recaptchaV3Service.execute(action).pipe(
      switchMap((value: any) => {
        const payload = {
        token: value
        };
        this.http.post(
          'http://127.0.0.1:8000/verify/recaptcha', payload
        );

      }));
  }

what I don't understand is why am I getting the error. What am I doing wrong?


Solution

  • SwitchMap should return an Observable, modify you code as following

    verifyinteraction(action): Observable<any>{
        return this.recaptchaV3Service.execute(action).pipe(
          switchMap((value: any) => {
            const payload = {
            token: value
            };
            return this.http.post(
              'http://127.0.0.1:8000/verify/recaptcha', payload
            );
    
          }));
      }