Javascript then方法中的代码不被执行,错误处理被执行

Javascript then方法中的代码不被执行,错误处理被执行,javascript,reactjs,firebase,asynchronous,google-cloud-firestore,Javascript,Reactjs,Firebase,Asynchronous,Google Cloud Firestore,我正在React和firebase中开发一个web应用程序,但很难让它正常工作 这是我的密码 import React,{useRef,useState}来自“React” 从“react bootstrap”导入{表单、按钮、卡片、警报} 从“./contexts/AuthContext”导入{useAuth} 从“react router dom”导入{Link,useHistory,Redirect,Route} 从“./firebase”导入{db} 从“./组件/仪表板”导入仪表板 导

我正在React和firebase中开发一个web应用程序,但很难让它正常工作

这是我的密码

import React,{useRef,useState}来自“React”
从“react bootstrap”导入{表单、按钮、卡片、警报}
从“./contexts/AuthContext”导入{useAuth}
从“react router dom”导入{Link,useHistory,Redirect,Route}
从“./firebase”导入{db}
从“./组件/仪表板”导入仪表板
导出默认函数UpdateProfile(){
const usernameRef=useRef()
const emailRef=useRef()
const passwordRef=useRef()
const passwordConfirmRef=useRef()
const{updateUser,currentUser,updatePassword}=useAuth()
常量[error,setError]=useState(“”)
常量[loading,setLoading]=useState(false)
const history=useHistory()
函数handleSubmit(e){
e、 预防默认值()
if(passwordRef.current.value!==passwordConfirmRef.current.value){
return setError(“密码不匹配”)
}
if(passwordRef.current.value){
updatePassword(passwordRef.current.value)
}
const uid=currentUser.uid
db.collection('users').doc(uid.get())
。然后(快照=>{
const data=snapshot.data()
试一试{
设置加载(真)
设置错误(“”)
updateUser(usernameRef.current.value、emailRef.current.value、数据)
history.push(“/dashboard”)
}抓住{
setError(“更新帐户失败”)
}
设置加载(错误)
})
}
返回(
更新配置文件
{error&&{error}
名称
电子邮件
密码
密码确认
更新
取消
)
}
这是用户编辑功能的代码:在表单中输入一个值,然后按按钮运行handleSubmit

function updateUser(用户名、电子邮件、数据){
const uid=data.uid
db.collection('users').doc(uid).set({
电邮:电邮,,
用户名:用户名,
},{merge:true})
}
useffect(()=>{
const unsubscribe=auth.onAuthStateChanged(异步(用户)=>{
如果(用户){
const uid=user.uid
控制台日志(uid)
等待db.collection('users').doc(uid.get())
。然后(快照=>{
const data=snapshot.data()
setCurrentUser(数据)
设置加载(错误)
})
}
})
退订
}, [])
执行此updateUser函数后,下面的代码重写firestore数据,我们想做一个history.push,然后将handleSubmit重定向到/dashboard,但我们想让控制台在控制台中说“success!”和“failure!”!在控制台中,应用程序上会显示消息“success!”

当我查看firestore数据时,我发现我输入的值正确地反映在firestore中

这告诉我handleSubmit的then部分不起作用,但我不知道为什么不起作用

如果你有解决办法,我很想听听。 谢谢。

你需要

  • await
    或将一个
    .catch
    放在
    updateUser
    承诺链上(如果承诺是
    await
    的话,它周围的
    try
    /
    catch
    只会捕获异步错误)
  • updateUser
  • 将函数传递给
    。然后
    回调-您的
    。然后(console.log(“success!!”)
    立即调用
    控制台。log
    并将
    未定义的
    传递给
    。然后

非常感谢。成功了。但是,在重定向到/dashboard后,我想反映我输入的值,但它反映了我输入之前的值。重定向到/dashboard后,如何使值反映出来?您所说的
是什么意思?要反映我输入的值吗?您是否在某处或某处记录了值?抱歉,没有解释。我添加了一些代码。此handleSubmit函数用于编辑用户信息的组件。当您在表单中输入值并单击提交按钮时,handleSubmit函数输入的值将传递给updateUser函数,该函数随后引用useEffect中firebase集合中的数据,并将其显示在应用程序上。但是,表单中输入的值会反映在firestore中,但在重定向后,用户信息会在编辑前显示。
function handleSubmit(e) {
    e.preventDefault()
    if (passwordRef.current.value !== passwordConfirmRef.current.value) {
        return setError("Passwords do not match")
    }

    if (passwordRef.current.value) {
        updatePassword(passwordRef.current.value)
    }

    const uid = currentUser.uid
    db.collection('users').doc(uid).get()
        .then(snapshot => {
            const data = snapshot.data()
            setLoading(true)
            setError("")
            return updateUser(usernameRef.current.value, emailRef.current.value, data);
        })
        .then(() => {
            // Success
            history.push('/dashboard')
        })
        .catch((error) => {
            setError("failed!!")
        })
        .finally(() => {
            setLoading(false)
        });
}
function updateUser(username, email, data) {
    const uid = data.uid
    return db.collection('users').doc(uid).set({
        email: email,
        username: username,
    }, { merge: true })
        .then(() => console.log("success!!"))
}