在反应中使用 pathSegments 时,Firestore getDoc 将无法工作

Firestore getDoc wont work when using pathSegments in react

我正在尝试从 firebase 执行非常简单的数据加载,但是当我在 react 中使用路径段时它不会工作

一些非常简单的东西

import { getFirestore, getDoc, getDocs, setDoc, doc, updateDoc, getCollection, collection } from "firebase/firestore";
import { firebaseConfig } from "config";

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const firestore = getFirestore(app);


getDoc(doc(firestore, 'collection', ['path','to','document']));

会抛出异常

Uncaught (in promise) TypeError: n.split is not a function

我该怎么做才能为该函数传递一个数组?

数组上的doc() doesn't take an array as param but path segments (strings). Try using the spread syntax如下图:

getDoc(doc(firestore, 'collection', ...['path','to','document']));
// spread syntax here                ⬆


// Equivalent to:
getDoc(doc(firestore, 'collection', 'path', 'to', 'document'));

doc() 文档中的函数签名:

export declare function doc(firestore: Firestore, path: string, ...pathSegments: string[]): DocumentReference<DocumentData>;