OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I query firestore data that is inside collections in said document?

  • Thread starter Thread starter Epimetheus14
  • Start date Start date
E

Epimetheus14

Guest
I am trying to run a query to see if a user's document contains a product ID inside of the document but the problem is that the product ID is stored within its own dropdown collection and not in the first data group, so what I need to do is reach down but the problem is that there can be more than one product like this that all have autogenerating id's when they are created. I am trying to query this to make sure the user has purchased the product before displaying content on it in a v-if statement in vue.

enter image description here As you can see there are multiple documents within the payments collection and the product I am after is first under items and then under price before I can get there, ordinarily I would just navigate right down to the product but I can't since the payments collection documents autogenerate and have autogenerating ids.

What I have tried doing is this below.

Code:
<script>
import { projectFirestore } from '../../Firebase/Config'
import PriceSelection1 from "./CowgirlVid/PriceSelection1.vue";
import SubscribedAccountSignup1 from "./CowgirlVid/SubscribedAccountSighnup1.vue";
import { firebaseAuth } from "../../Firebase/firebase";
import { ref } from 'vue'
import {
  getFirestore,
  collection,
  query,
  where,
  getDocs,
} from "firebase/firestore";
export default {
    components: {
    PriceSelection1,
    SubscribedAccountSignup1
  },
  data() {
    return {
      isLoading: false,
      Purchased2: null,
    };
  },

  mounted() {
    this.fetchSubscription();
  },

  methods: {
    async fetchSubscription() {
      this.isLoading = true;
      const db = getFirestore();
      const subsRef = collection(
        db,
        "Subscribed", firebaseAuth.currentUser.uid, "payments"
      );
      const subsQuery = query(
        subsRef, 
        where("product", "==", "prod_QIRNX4klvLTfKe" )
      );

      this.subscription = await getDocs(subsQuery)
        .then((sub) => 
            sub.docs.length > 0 ? sub.docs[0].data() : null);

      this.isLoading = false;
    },
  },

Ignore the subscription it doesn't mean a subscription we are dealing with stripe one time products that I am trying to get the id off I know that subscription products have the id on the top level but these are unfortunately one time products which do not.

I have also tried using collectionGroup queries from the firebase docs like below

Code:
  methods: {
    async fetchSubscription() {
      this.isLoading = true;
      const db = getFirestore();
      const subsRef = collection(
        db,
        "Subscribed", firebaseAuth.currentUser.uid, "payments"
      );
      const subsQuery = query(collectionGroup(
        subsRef, "items", "price" ),
        where("product", "==", "prod_QIRNX4klvLTfKe" )
      );

      this.subscription = await getDocs(subsQuery)
        .then((sub) => 
            sub.docs.length > 0 ? sub.docs[0].data() : null);

      this.isLoading = false;
    },
  },

Yes, I have imported collectionGroup I just haven't included the top of my code in this snippet as it hasn't changed.

Does anyone have any idea how to get the product id by using collectionGroup or some other means? I would greatly appreciate any help.

<p>I am trying to run a query to see if a user's document contains a product ID inside of the document but the problem is that the product ID is stored within its own dropdown collection and not in the first data group, so what I need to do is reach down but the problem is that there can be more than one product like this that all have autogenerating id's when they are created. I am trying to query this to make sure the user has purchased the product before displaying content on it in a v-if statement in vue.</p>
<p><a href="https://i.sstatic.net/XWjo5acg.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/XWjo5acg.png" alt="enter image description here" /></a>
As you can see there are multiple documents within the payments collection and the product I am after is first under items and then under price before I can get there, ordinarily I would just navigate right down to the product but I can't since the payments collection documents autogenerate and have autogenerating ids.</p>
<p>What I have tried doing is this below.</p>
<pre><code><script>
import { projectFirestore } from '../../Firebase/Config'
import PriceSelection1 from "./CowgirlVid/PriceSelection1.vue";
import SubscribedAccountSignup1 from "./CowgirlVid/SubscribedAccountSighnup1.vue";
import { firebaseAuth } from "../../Firebase/firebase";
import { ref } from 'vue'
import {
getFirestore,
collection,
query,
where,
getDocs,
} from "firebase/firestore";
export default {
components: {
PriceSelection1,
SubscribedAccountSignup1
},
data() {
return {
isLoading: false,
Purchased2: null,
};
},

mounted() {
this.fetchSubscription();
},

methods: {
async fetchSubscription() {
this.isLoading = true;
const db = getFirestore();
const subsRef = collection(
db,
"Subscribed", firebaseAuth.currentUser.uid, "payments"
);
const subsQuery = query(
subsRef,
where("product", "==", "prod_QIRNX4klvLTfKe" )
);

this.subscription = await getDocs(subsQuery)
.then((sub) =>
sub.docs.length > 0 ? sub.docs[0].data() : null);

this.isLoading = false;
},
},
</code></pre>
<p>Ignore the subscription it doesn't mean a subscription we are dealing with stripe one time products that I am trying to get the id off I know that subscription products have the id on the top level but these are unfortunately one time products which do not.</p>
<p>I have also tried using collectionGroup queries from the firebase docs like below</p>
<pre><code> methods: {
async fetchSubscription() {
this.isLoading = true;
const db = getFirestore();
const subsRef = collection(
db,
"Subscribed", firebaseAuth.currentUser.uid, "payments"
);
const subsQuery = query(collectionGroup(
subsRef, "items", "price" ),
where("product", "==", "prod_QIRNX4klvLTfKe" )
);

this.subscription = await getDocs(subsQuery)
.then((sub) =>
sub.docs.length > 0 ? sub.docs[0].data() : null);

this.isLoading = false;
},
},

</code></pre>
<p>Yes, I have imported collectionGroup I just haven't included the top of my code in this snippet as it hasn't changed.</p>
<p>Does anyone have any idea how to get the product id by using collectionGroup or some other means? I would greatly appreciate any help.</p>
 

Latest posts

Top