Firebase认证报错:Property 'firebase'不存在,求技术解决方案
解决"Property 'firebase' doesn't exist"错误
问题定位
错误根源在代码中调用firebase.firestore.FieldValue.serverTimestamp()的位置——当前Firebase SDK(v9及以上版本)采用模块化架构,不再提供全局firebase对象,直接引用全局firebase导致了ReferenceError。
修复步骤
- 导入正确的Firestore模块
在组件文件顶部添加serverTimestamp的模块化导入:
import { serverTimestamp } from "firebase/firestore";
- 替换全局引用代码
将setDoc中的firebase.firestore.FieldValue.serverTimestamp()替换为导入的serverTimestamp():
setDoc(doc(db, `users`, auth.currentUser.uid), { email, convoCode, id: createdUser.user.uid, timestamp: serverTimestamp(), // 替换此处 bio: "I don't got a bio🥱", image: 'https://firebasestorage.googleapis.com/v0/b/convoapp-91392.appspot.com/o/4112f225-1b3f-47a3-b2eb-d2b99dfd3996.jpg?alt=media&token=5c8177f3-51f7-4d77-a761-ef6b5c6ce1a0', })
额外优化建议
- 检查Firebase初始化文件,确保所有功能都通过模块化导入引入,避免依赖全局对象
- 你的
signUp是async函数,建议用try/catch替代.then/.catch嵌套,同时修复getDocs返回值的访问错误:
const signUp = async () => { try { const userSnapshot = await getDocs(createdUserDoc); const userData = userSnapshot.docs[0]?.data(); if(userData?.convoCode === convoCode){ setConvoCode(Math.random().toString(36).slice(6)) } const createdUser = await auth.createUserWithEmailAndPassword(email, password); await setDoc(doc(db, `users`, createdUser.user.uid), { email, convoCode, id: createdUser.user.uid, timestamp: serverTimestamp(), bio: "I don't got a bio🥱", image: 'https://firebasestorage.googleapis.com/v0/b/convoapp-91392.appspot.com/o/4112f225-1b3f-47a3-b2eb-d2b99dfd3996.jpg?alt=media&token=5c8177f3-51f7-4d77-a761-ef6b5c6ce1a0', }); router.push('/authentication/AddUsernameScreen'); setPassword(''); setEmail(''); } catch (err) { toastRef.current.show({ type: 'error', text: `${err.message}`, duration: 5000 }); console.log(err); } }
内容的提问来源于stack exchange,提问作者mac
相关产品推荐
相关产品推荐

