简体   繁体   English

Firebase 使用 typescript 的 auth.currentUser 错误

[英]Firebase auth.currentUser error using typescript

When configuring typescript with firebase, I am getting the following error: Argument of type 'User | null' is not assignable to parameter of type 'User'. Type 'null' is not assignable to type 'User'.使用 firebase 配置Argument of type 'User | null' is not assignable to parameter of type 'User'. Type 'null' is not assignable to type 'User'.时,出现以下错误: Argument of type 'User | null' is not assignable to parameter of type 'User'. Type 'null' is not assignable to type 'User'.

The error is here: auth.currentUser It is unclear to me what type it would be.错误在这里:auth.currentUser我不清楚它是什么类型。

updateProfile(auth.currentUser, {
                    displayName: name
                }

Full code:完整代码:

import { getAuth, createUserWithEmailAndPassword, signOut, onAuthStateChanged, signInWithEmailAndPassword, updateProfile } from "firebase/auth";
import { useEffect, useState } from "react";
import FirebaseInitialization from "../Firebase/Firebase.init";



FirebaseInitialization()

interface IUseFirebaseProps {
}

const UseFirebase: React.FunctionComponent<IUseFirebaseProps> = (props) => {
    const [user, setUser] = useState({})
    const [error, setError] = useState('')
    const [admin, setAdmin] = useState(false)
    const [isLoading, setIsLoading] = useState(true)
    const auth = getAuth()


      // REGISTER WITH EMAIL AND PASSWORD
      const RegisterUser = (email: string, password:string, name:string, history: any) => {
        setIsLoading(true)
        createUserWithEmailAndPassword(auth, email, password)
            .then((userCredential) => {
                const newUser = { email, displayName: name };
                setUser(newUser);
                setError('')
                updateProfile(auth.currentUser, {
                    displayName: name
                }).then(() => {
                }).catch((error) => {
                });
                history.replace('/');
            })
            .catch((error) => {

                const errorMessage = error.message;
                setError(errorMessage)
            }).finally(() => setIsLoading(false));
    }
  return <h2>Firebase</h2>
};

export default UseFirebase;

How can I solve this?我该如何解决这个问题? can anyone help me?谁能帮我?

The value of auth.currentUser can be null . auth.currentUser的值可以是null In this case you can pass User object directly from UserCredential object. Try refactoring the code as shown below:在这种情况下,您可以直接从UserCredential object 传递User object。尝试重构代码,如下所示:

const RegisterUser = async (email: string, password: string, name: string, history: any) => {
  setIsLoading(true)
  const userCredential = await createUserWithEmailAndPassword(auth, email, password)
  
  const newUser = {
    email,
    displayName: name
  };
  
  setUser(newUser);
  setError('')
  
  // Pass userCredential.user instead of auth.currentUser
  updateProfile(userCredential.user, {
    displayName: name
  }).then(() => {}).catch((error) => {});
    history.replace('/');
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 auth.currentUser 在 Firebase 的页面刷新上是 null - why auth.currentUser is null on page refresh in Firebase Authetication auth.currentUser 在主页上显示未定义 - auth.currentUser shows undefined on HomePage useEffect 不更新 auth.currentUser 更改 - useEffect not updating on auth.currentUser change 注册后直到我重新加载页面才获得 auth.currentUser (VUE3 + Quasar + Firebase) - Not getting auth.currentUser after registration until I reload the page (VUE3 + Quasar + Firebase) React-Native-Firebase Spread auth().currentUser Object {...auth().currentUser} - React-Native-Firebase Spread auth().currentUser Object {...auth().currentUser} firebase.auth().currentUser 在页面加载时为 null - firebase.auth().currentUser is null at page load 如何异步/等待 firebase.auth().currentUser? - How to async/await on firebase.auth().currentUser? Nuxtjs 和 Firebase Auth: await firebase.auth().currentUser 不等待? - Nuxtjs and Firebase Auth: await firebase.auth().currentUser not waiting? 如何使用 firebase_auth 包修复 flutter 应用程序中的 FirebaseAuth.instance.currentUser()? 它总是返回 null - How to fix FirebaseAuth.instance.currentUser() in flutter app using firebase_auth package? It is always returning null 当页面重新加载时,firebase.auth.currentUser 返回 null - When the page is reloaded, firebase.auth.currentUser returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM