【问题标题】:"ERROR db._checkNotDeleted is not a function" when trying to update my database尝试更新我的数据库时“错误 db._checkNotDeleted 不是函数”
【发布时间】:2022-01-21 08:18:12
【问题描述】:

我正在尝试使用 firebase 为我的网站创建简单的登录功能,但是我收到一条错误消息: ERROR db._checkNotDeleted is not a function

我的 Firebase 版本是 9,React 在 17.0.2 上运行。

这是我的 firebase 配置文件 sn-p(将隐藏配置,因为它是不必要的):

import { initializeApp } from "firebase/app";
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { getDatabase, ref, set } from "firebase/database";
import { getFirestore, doc, getDoc, collection, addDoc, getDocs, query, where} from "firebase/firestore";



const firebaseConfig = {
  */*/*/*/
};

const app = initializeApp(firebaseConfig);


export const auth = getAuth(app);

  export const createUserProfileDocument = async (userAuth, additionalData) => {
    if(!userAuth) return;

    const docRef = doc(db, "users", `${userAuth.uid}`);
    
    const docSnap = await getDoc(docRef);

    if (!docSnap.exists()) {
      const { displayName, email } = userAuth;
      const createdAt = new Date();
    
     
      try {
        await set(ref(db, 'users/' + `${userAuth.uid}`), {
          displayName: "TEST"
        })
        console.log("HERE");
       
      } catch (error) {
        console.log("ERROR", error.message);
      }
    }
    return docRef;
  }

  
  

  const provider = new GoogleAuthProvider();

  export const signInWithGoogle = () => {
      signInWithPopup(auth, provider)
      .then((result) => {
            console.log(result);
      }).catch((error) => {
          console.log(error);
      })
  }
 
  
  
  const db = getFirestore();

这是我的项目:https://github.com/poolpy111/oto-clothing/tree/master/src

在调试器中,错误行出现在第 43 行,这是我使用 firebase 的“ref”方法的地方。 我该怎么办?

【问题讨论】:

  • 用项目链接更新了我的帖子。谢谢
  • 您要使用哪个数据库?您正在创建文档引用(来自 Firestore),但使用来自 Firebase 实时数据库的 set()
  • 我不知道什么是实时数据库。正如控制台中所说,我正在使用“Firestore”数据库。在最新的文档中,sn-p 中有“ref”,但是它不起作用。我做错了什么?
  • 尝试按照我的回答重构代码。您基本上使用了实时数据库 SDK 的 set() 添加了一个 Firestore 文档,这肯定是行不通的。

标签: javascript reactjs firebase google-cloud-firestore


【解决方案1】:

Firebase 有两个数据库,即 Firebase Realtime DatabaseCloud Firestore。在提供的代码中,您使用doc() 创建DocumentReference,但随后使用从实时数据库SDK 导入的set(),这会导致错误。

如果要在 Firestore 中添加数据,则从 Firestore 导入 setDoc() 并重构代码,如下所示:

import { initializeApp } from "firebase/app";
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { getFirestore, doc, getDoc, setDoc, collection, addDoc, getDocs, query, where} from "firebase/firestore";

export const createUserProfileDocument = async (userAuth, additionalData) => {
  if(!userAuth) return;

  const docRef = doc(db, "users", `${userAuth.uid}`);
    
  const docSnap = await getDoc(docRef);

  if (!docSnap.exists()) {
    const { displayName, email } = userAuth;
    const createdAt = new Date();  
     
    try {
      // use setDoc()
      await setDoc(docRef, {
        displayName: "TEST"
      })
      console.log("HERE");   
    } catch (error) {
      console.log("ERROR", error.message);
    }
  }
  return docRef;
}

关注Firestore's documentation了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2021-07-01
    • 2022-11-28
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    相关资源
    最近更新 更多