You first need to use doc()
function to create a DocumentReference
for the documents and then use setDoc()
function to add document in Firestore as mentioned in the documentation.
import { doc, setDoc } from "firebase/firestore"
// here db is getFirestore()
const docRef = doc(db, `User/${fields.user}/Address/${fields.address}`)
await setDoc(docRef, { test: "test" })
Alternatively you can use a batched write to add both the documents at once. Try refactoring the code as shown below:
import {
writeBatch,
doc
} from "firebase/firestore";
// Get a new write batch
const batch = writeBatch(db);
const docRef = doc(db, `User/${fields.user}/Address/${fields.address}`);
batch.set(docRef, {
User: fields.user,
Address: fields.address
});
const subDocRef = doc(db, `User/${fields.user}/Address/${fields.address}/Orders/${fields.ID}`);
batch.update(subDocRef, {
ID: fields.ID
});
// Commit the batch
batch.commit().then(() => {
console.log("Documents added")
}).catch(e => console.log(e));
Also checkout: Firestore: What's the pattern for adding new data in Web v9?