Since the data received from a firestore document does not include the sub-collections attached to the document itself, I need to access the sub-collection in a different way.
Task I want to accomplish :
-
Retrieve subcollection documents of only some specific primary documents;
-
Ids of primary documents I want to access are store in an array of object, and this array come from an other Vuex module. I'm using root context to retrieves this state;
-
I iterate over array and get firestore data for each demand id;
At this point, main reason why my approach descripe here doesn't work, is that I don't retrieve correctly state from Module A to Module B.
Also, I'm not sure if I can iterate over db like I do. I also tried an approach like : db.collection().where().collection().get()
It would be appreciate if someone could direct me in a better way to accomplish these tasks.
FIRESTORE
Demands (primary collection) Demand 1 (primary doc) Meetings (subcollection) Meeting 1 (subdoc) Meeting 2 (subdoc) … Demand 2 (primary doc) Demand 3 (primary doc) …
APP – Vue.js Component – dashboard.vue
computed: { demands() { return this.$store.state.demands.demands; }, meetings() { return this.$store.state.meetings.meetings; } }, created() { this.$store.dispatch("demands/getDemands"); this.$store.dispatch("meetings/getMeetings"); },
VUEX store Module A – demands.js
export default { namespaced: true, state: { demands:[], sendDemands: [], }, actions: { // Create demand in firestore createDemand({ commit }, demand) { demand.status = "pending" demand.createdate = Timestamp.fromDate(new Date()) console.log(demand) return db .collection('demands') .add(demand) .then(docRef => { demand.id = docRef.id commit('auth/addDemandToUser', demand, { root: true }) return true }) }, // Get demands from firestore getDemands({rootState, commit}) { const { uid } = rootState.auth.user if (!uid) return Promise.reject('User is not logged in!') const userRef = db.collection('profiles').doc(uid) return db .collection('demands') .where('toUser', "==", userRef) .get() .then(async snapshot => { const demands = await Promise.all( snapshot.docs.map(doc => extractDataFromDemand({id: doc.id, demand: doc.data()}) ) ) commit('setDemands', {resource: 'demands', demands}) return demands }) }, ... mutations: { setDemands(state, { resource, demands }) { state[resource] = demands }, ...
Module B – meeting.js
export default { namespaced: true, state: { meetings:[], sendMeeting: [], demands: [] }, actions: { // Create meeting in firestore createMeeting( { commit }, meeting) { meeting.createdate = Timestamp.fromDate(new Date()) return db .collection('demands') .doc(meeting.demandId) .collection('meetings') .add(meeting) .then(docRef => { meeting.id = docRef.id commit('auth/addMeetingToUser', meeting, { root: true }) return true }) }, // Get meeting from firestore getMeetings({rootState, commit}) { const meetings = [] // Get list of demands from "demands" store module : const demands = rootState.demands.demands console.log(demands) const nbItems = demands.length console.log(nbItems) for (var i = 0; i = demands.length - 1; i++) { return db .collection('demands') .doc(demands[i].id) .collection('meetings') .get() .then(function(querySnapshot) { querySnapshot.forEach(function(doc) { meetings.push(doc.data) }) commit('setMeetings', {resource: 'meetings', meetings}) return meetings }) } }, ... mutations: { setMeetings(state, { resource, meetings }) { state[resource] = meetings }, ...
https://stackoverflow.com/questions/65911613/how-to-retrieve-firestore-sub-collection-documents-belonging-to-specific-primary January 27, 2021 at 09:21AM
没有评论:
发表评论