Reactjs 如果完成OTP验证,则将数据保存到firebase实时数据库
我的代码如上所示。我希望执行时,在提交表单后,将调用if Submit()。然后它将检查OTP验证。如果成功,则只有数据将存储在firebase实时数据库中Reactjs 如果完成OTP验证,则将数据保存到firebase实时数据库,reactjs,firebase,firebase-realtime-database,firebase-authentication,react-router,Reactjs,Firebase,Firebase Realtime Database,Firebase Authentication,React Router,我的代码如上所示。我希望执行时,在提交表单后,将调用if Submit()。然后它将检查OTP验证。如果成功,则只有数据将存储在firebase实时数据库中 列表项 为什么不把保存数据的逻辑放在确认承诺之后: enter code here Submit=e=>{ e.preventDefault(); let data=""; this.setUpRecaptcha(); // window.alert("Appointment
为什么不把保存数据的逻辑放在确认承诺之后:
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");
}
};
我已经做到了。但当我这样做时,它会向我发送OTP,但在确认时,它会提醒我“OTP验证失败”。表示确认结果。虽然我输入了正确的OTP,但确认(代码)不能正常工作。我不知道为什么会发生这种情况。您是否尝试对捕获到的错误进行console.log()处理?它可能包含更多的错误信息。当我尝试console.log()时,它显示“Uncaught(in promise)null”。但首先,它会提醒我“OTP已验证!!!”然后提醒我“OTP验证失败”。我尝试了一些不同的方法。但每当我在“confirmationResult.confirm(code).then(function(result)”这个块中写入任何逻辑时,它就会给我一个错误,并提醒我“OTP验证失败”。请帮助我解决此问题。我为标题和说明中提出的问题提供了答案。您应该为firebase问题创建不同的问题。
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")
});
};