基本上我想做的就是在我的檔案中創建一個子集合,但是使用 webSDK 9,即可搖樹的 SDK。
我找到了解決方案,但它們都已過時并使用舊的 sdk。
如果這有幫助,我正在使用 NextJS 和 ES6。
uj5u.com熱心網友回復:
您需要使用DocumentReference要添加子集合的 Firestore 檔案,如下所示:
const docRef = ....;
const docSubcollectionRef = collection(docRef, 'theSubColl');
await addDoc(docSubcollectionRef, {foo: 'bar'});
更具體地說,如果theTempoColl集合中有一個 id 為doc1(ie docRef = doc(db, 'theTempoColl', 'doc1')) 的檔案,則上面的代碼將生成一個具有以下路徑的檔案:theTempoColl/doc1/theSubColl/{auto_generated_Id}
uj5u.com熱心網友回復:
如果您定義特定的檔案 ID,您也可以使用 setDoc。
import {db} from './config';
import {setDoc, doc } from '@firebase/firestore'
export default function Page(){
const createDocWithSpecificId = async () => {
cosnt docRef = doc(db, 'collection_name', 'specific_id');
const createDoc = await setDoc(docRef, {foo : 'bar'});
};
}
謝謝你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/420631.html
標籤:
上一篇:打算重定向活動時無法決議建構式
