I'm in the process of trying to reference an auto-generated firestore document ID in order to create a subcollection within it.
The issue I'm facing is that although I can locate the document ID, I'm struggling to save it to a variable in a usable format.
Currently, I am retrieving a specific document for the current user. The document ID is stored in userDocs
after calling the getCollection
composable function.
const { documents: userDocs } = getCollection("userData", [
"userId",
"==",
user.value.uid,
]);
When I attempt to log userDocs
, I receive an object containing all the document information, including the document id
.
However, despite my efforts to access the ID from the object using expressions like userDocs.value
or userDocs.id
, I only get undefined
as the result.
If I use a v-for
loop in the HTML like this:
<div v-for="docs in userDocs" :key="docs.id">
<h1>{{ docs.id }}</h1>
</div>
It successfully displays the document ID on the webpage.
My goal is to fetch the document ID here so that I can add a subcollection to this specific user's document in Firebase.
const addNew = async (plant) => {
const userPlant = {
plant: plant,
planted: tempPlanted,
};
const res = await projectFirestore
.collection("userData")
.doc(userDocs) <--------HERE
.collection("plants")
.add(userPlant);
};
Any assistance would be greatly appreciated.