简体   繁体   English

某些文档的 firestore 快照

[英]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. JS setupLocations 函数循环遍历并将数据添加到 UI 上的 ul/li。它可以工作,但它加载每个文档,我只想要一个基于用户集合中列出的用户访问权限的特定文档。

In my "locations-data" collection I have documents named by location (eg ca1001, ca1002, etc.)在我的“位置数据”集合中,我有按位置命名的文档(例如 ca1001、ca1002 等)

And a user collection > userID > locations array to set which locations a user can view.还有一个user collection > userID > locations数组来设置用户可以查看哪些位置。

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.如上所述,安装程序在前端运行良好,但会加载所有文档,我只希望某些用户获取某些位置(文档)以显示在 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.我将用户集合中的位置数组放入一个 var 中,并尝试在代码端使用 map 和其他东西,还通过安全规则等尝试了一些东西。只是运气不佳。

Here's the code on the UI side that loads the display for the user.这是 UI 端为用户加载显示的代码。

 // 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.我最终使用了“array-contains”,并在我想要包含的每个文档中添加了一个用户字段。 .where('_users', 'array-contains', user.email). .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.由于您的第一个选择是根据用户位置获取所有文档,因此您可以使用 @pepe 所说的,查询过滤器中的where来获取集合中的特定或某些文档。 The other option is store all the data of location from user collection to a array variable then display it directly to your UI.另一个选项是将user集合中的所有位置数据存储到数组变量中,然后将其直接显示到您的 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.有了它,您就不需要查询location-data收集,因为您已经根据用户的数据获得了所有特定的位置数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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