简体   繁体   中英

firestore snapshot of certain documents

Any suggestions on how to get a snapshot to only return certain documents in a collection?

 db.collection('locations-data').get().then(snapshot => { setupLocations(snapshot.docs);

The JS setupLocations function loops through and adds data to an ul/li on the UI.It Works but it loads every document and I only want a specific document based on a users access listed in a user collection.

In my "locations-data" collection I have documents named by location (eg ca1001, ca1002, etc.)

And a user collection > userID > locations array to set which locations a user can view.

As mentioned above, The Setup works great on the front-end but loads all documents and I only want certain users to get certain locations (documents) to show in the UI.

I get the locations array from the users collection into a var, and have tried using map and other things on the code side, also tried a few things via security rules, etc. Just not having much luck.

Here's the code on the UI side that loads the display for the user.

 // show locations const setupLocations = (data) => { if (data.length) { let html = ''; data.forEach((doc) => { const location = doc.data(); const li = ` <li> <div class="collapsible-header grey lighten-4"> ${location.shortName}, Total Income: ${location.todayIncome} </div> <div class="collapsible-body white">${location.totalIncome}</div> </li> `; html += li; }); locationList.innerHTML = html; } else { locationList.innerHTML = '<h5 class="center-align">Login to view data</h5>'; } };

I ended up using "array-contains" and just added a users field to each document I want to include. .where('_users', 'array-contains', user.email). Will just have to create a simple admin to manage users this way to avoid having to add all users to each document manually. Thanks all for suggestions.

Any suggestions on how to get a snapshot to only return certain documents in a collection?

Since your first option is to get all document based on the users location you can use what @pepe said, the where in query filter to get a specific or certain documents inside your collection. The other option is store all the data of location from user collection to a array variable then display it directly to your UI. With that you don't need to query for location-data collection because you already got all the specific location data based on the user's data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM