You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase认证报错:Property 'firebase'不存在,求技术解决方案

解决"Property 'firebase' doesn't exist"错误

问题定位

错误根源在代码中调用firebase.firestore.FieldValue.serverTimestamp()的位置——当前Firebase SDK(v9及以上版本)采用模块化架构,不再提供全局firebase对象,直接引用全局firebase导致了ReferenceError。

修复步骤

  1. 导入正确的Firestore模块
    在组件文件顶部添加serverTimestamp的模块化导入:
import { serverTimestamp } from "firebase/firestore";
  1. 替换全局引用代码
    将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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 09:05:31