首页 > 解决方案 > 未处理的承诺拒绝:您在预期流的位置提供了“未定义”。使用 SwitchMap 和 Observable 类型的函数

问题描述

这是我第一次使用switchMaprxjs 库。

我猜这个错误信息意味着它对我做事的方式不满意。

观察我的addemail功能

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);
              }
            );
        });

  }

更具体地说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
        );

      }));
  }

我不明白为什么我会收到错误消息。我究竟做错了什么?

标签: angularrxjsrecaptcharxjs-observables

解决方案


SwitchMap应该返回一个 Observable,修改你的代码如下

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
        );

      }));
  }

推荐阅读