Basically all I want to do is create a subcollection in my document, but using the webSDK 9, the tree-shakable SDK.
I've found solutions, but all of them are outdated and use the old sdk.
If this helps, I'm using NextJS and ES6.
CodePudding user response:
You need to use the DocumentReference
of the Firestore doc to which you want to add a subcollection, as follows:
const docRef = ....;
const docSubcollectionRef = collection(docRef, 'theSubColl');
await addDoc(docSubcollectionRef, {foo: 'bar'});
More concretely, if you have a doc in the theTempoColl
collection with id doc1
(i.e. docRef = doc(db, 'theTempoColl', 'doc1')
), the above code will generate a document with the following path: theTempoColl/doc1/theSubColl/{auto_generated_Id}
CodePudding user response:
You can use also setDoc if you define specific document id.
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'});
};
}
Thank you.