【问题标题】:firestore.collection is not a functionfirestore.collection 不是函数
【发布时间】:2022-01-07 14:40:20
【问题描述】:

这是我的firebase.js

import { initializeApp } from "firebase/app";

import { getFirestore } from "firebase/firestore";

import { getAuth } from "firebase/auth";
import { getAnalytics, logEvent, isSupported } from "firebase/analytics";

//  Update the config
var firebaseConfig = {
  // ...Update config
 
};

// Initialize Firebase

const app = initializeApp(firebaseConfig);
const firestore = getFirestore(app);
const auth = getAuth();
const analytics = getAnalytics(app);

// Uncomment the following if you want to use emulator
// if (process.env.NODE_ENV === "development") {
//   firestore.useEmulator("localhost", 8080);
//   auth.useEmulator("http://localhost:9099");
// }

export { app, firestore, auth, analytics };

这就是我在我的一个组件中使用它的方式-

import { app, firestore, auth } from "../../firebase";

  const linksPathRef = useMemo(
    () => firestore.collection("users").doc(userUid).collection("links"),
    [userUid]
  );

但我收到firestore.collection is not a function 错误。不确定,我哪里出错了。

项目中安装的firebase版本"firebase": "^9.6.1"

【问题讨论】:

标签: javascript reactjs firebase google-cloud-firestore react-hooks


【解决方案1】:

正如评论者所指出的,您在导入 v9 SDK 时使用的是较旧的 API 语法。

消息是关于这一行的

firestore.collection("users").doc(userUid).collection("links")

在新的模块化语法中,应该是:

collection(firestore, "users", userUid, "links")

我建议再次阅读 Firestore 文档,并查看此 upgrade guide

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 2017-05-06
    • 2019-12-20
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    相关资源
    最近更新 更多