【问题标题】:Save data to firebase realtime database if OTP verification is done如果 OTP 验证完成,将数据保存到 firebase 实时数据库
【发布时间】:2020-09-18 17:53:57
【问题描述】:
enter code here



Submit=e=>{

      e.preventDefault();
        let data="";
      this.setUpRecaptcha();
      // window.alert("Appointment Done Successfully at time "+this.state.curTime+" ! Go to the home page");
      var phoneNumber = this.state.contact;
      console.log(phoneNumber)
      var appVerifier = window.recaptchaVerifier;
      firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
          .then(function (confirmationResult) {
            // SMS sent. Prompt user to type the code from the message, then sign the
            // user in with confirmationResult.confirm(code).

            window.confirmationResult = confirmationResult;
            var code = window.prompt("enter OTP")
            confirmationResult.confirm(code).then(function (result) {
              // User signed in successfully.
              var user = result.user;    
              alert("OTP is verified!!!");
              this.data="doneit";
              // ...
            }).catch(function (error) {
              // User couldn't sign in (bad verification code?)
              // ...
              alert("OTP verification failed");
            });
          }).catch(function (error) {
            // Error; SMS not sent
            // ...
            alert("cann't send OTP use another Contact number")
          });

//Data storage code
          if(this.data=='doneit'){
            firebase.database().ref("appoinment").push(
              {
                name:this.state.name,
                email:this.state.email,
                contact:this.state.contact,
                age:this.state.age,
                gender:this.state.gender,
                Appointdate:this.state.Appointdate,
                Description:this.state.Description,
                date:this.state.curTime
              }
            );
            alert(" Data saved successfully");
            window.location.reload(false);
          }
          else{
            alert("failed");
          }


    };

我的代码如上图所示。我希望以这样的方式执行,即如果在提交表单后将调用 Submit()。然后它将检查 OTP 验证。如果成功,则只有数据将存储在 firebase 实时数据库中

  1. 列表项

【问题讨论】:

    标签: reactjs firebase firebase-realtime-database firebase-authentication react-router


    【解决方案1】:

    为什么不把保存数据的逻辑放在确认承诺之后:

    Submit=e=>{
    
          e.preventDefault();
            let data="";
          this.setUpRecaptcha();
          // window.alert("Appointment Done Successfully at time "+this.state.curTime+" ! Go to the home page");
          var phoneNumber = this.state.contact;
          console.log(phoneNumber)
          var appVerifier = window.recaptchaVerifier;
          firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
              .then(function (confirmationResult) {
                // SMS sent. Prompt user to type the code from the message, then sign the
                // user in with confirmationResult.confirm(code).
    
                window.confirmationResult = confirmationResult;
                var code = window.prompt("enter OTP")
                confirmationResult.confirm(code).then(function (result) {
                  // User signed in successfully.
                  var user = result.user;    
                  alert("OTP is verified!!!");
                  this.data="doneit";
    
                     firebase.database().ref("appoinment").push({
                       name:this.state.name,
                       email:this.state.email,
                       contact:this.state.contact,
                       age:this.state.age,
                       gender:this.state.gender,
                       Appointdate:this.state.Appointdate,
                       Description:this.state.Description,
                       date:this.state.curTime
                    });
                   alert(" Data saved successfully");
                   window.location.reload(false);
    
    
                  // ...
                }).catch(function (error) {
                  // User couldn't sign in (bad verification code?)
                  // ...
                  alert("OTP verification failed");
                });
              }).catch(function (error) {
                // Error; SMS not sent
                // ...
                alert("cann't send OTP use another Contact number")
              });  
    
        };
    

    【讨论】:

    • 我已经做到了。但是当我这样做时,它会向我发送 OTP,但在确认时它会提醒我“OTP 验证失败”。意味着虽然我输入了正确的 OTP,但确认结果.确认(代码)不能正常工作。我不知道为什么会这样。
    • 您是否尝试过 console.log() 您正在捕获的错误?它可能包含更多信息,出了什么问题。
    • 当我尝试 console.log() 时,它显示“Uncaught (in promise) null”。但首先,它提醒我“OTP 已验证!!!”然后提醒我“OTP 验证失败”。
    • 我尝试了一些不同的方法。但是,每当我在“confirmationResult.confirm(code).then(function (result)”这个块中写任何逻辑时,它都会给我一个错误并提醒我“OTP 验证失败”。请帮我解决这个问题。
    • 我已经回答了标题和描述中提出的问题。您应该为 firebase 问题创建不同的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2020-10-10
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多