Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

402
Visualizações
Error de Firebase auth.currentUser usando mecanografiado

Al configurar TypeScript con Firebase, aparece el siguiente error: Argument of type 'User | null' is not assignable to parameter of type 'User'. Type 'null' is not assignable to type 'User'.

El error está aquí: auth.currentUser No tengo claro de qué tipo sería.

 updateProfile(auth.currentUser, { displayName: name }

Código completo:

 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;

¿Como puedo resolver esto? ¿Alguien puede ayudarme?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

El valor de auth.currentUser puede ser null . En este caso, puede pasar el objeto User directamente desde el objeto UserCredential . Intente refactorizar el código como se muestra a continuación:

 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('/'); }) }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda