简体   繁体   English

在 firestore 中查询数据并遍历数组时,有没有办法使用 where() function 和“array-contains”

[英]Is there a way to use the where() function and "array-contains" when querying data in firestore and iterate through an array

learning about firestore and making a simple event website to practice search queries where users can enter their city (selectedCity) and then select an event category such as "Music", "Comedy", "Bars" to filter the events.了解 firestore 并制作一个简单的活动网站来练习搜索查询,用户可以在其中输入他们的城市 (selectedCity),然后 select 一个活动类别,例如“音乐”、“喜剧”、“酒吧”来过滤活动。

I have an events array that is dynamic.我有一个动态的事件数组。 Users can click on the event types on the side of the screen and it populates the events array depending on which categories are selected.用户可以单击屏幕一侧的事件类型,它会根据选择的类别填充事件数组。 Ex: If I have selected to filter "Music", "Comedy", & "Bars", then the events array would contain those 3.例如:如果我选择过滤“音乐”、“喜剧”和“酒吧”,那么事件数组将包含这 3 个。

I was wondering if there was a way to iterate through the array to see if the category of the posted event stored in FireStore matches any of the events in the events array.我想知道是否有一种方法可以遍历数组以查看存储在 FireStore 中的已发布事件的类别是否与事件数组中的任何事件匹配。

Any tips on how to solve this problem would be helpful.有关如何解决此问题的任何提示都会有所帮助。 Right now I am just hardcoding events[0] which as "Music" in it to display all the music events现在我只是硬编码事件[0],其中作为“音乐”显示所有音乐事件

Here is my code for the query:这是我的查询代码:

    const [postsList, setPostsList] = useState([])
    const [value, setValue] = useState("");
    const [selectedCity, setSelectedCity] = useRecoilState(selectedCityState);
    const dbRef = collection(db, "posts");

    const [events, setEvents] = useState([
        "Music",
        "Comedy", 
        "Bars", 
      ])


const getPostsAPI = async () => {
        const data = [];
        const q = query(
                dbRef, 
                where("cityName", "==", selectedCity), 
                where("category", "array-contains", events[0]),
                orderBy("timestamp", "desc"));
        const posts = await onSnapshot(q, (querySnapshot) => {
             querySnapshot.docs.map(d => d.data().length)
            querySnapshot.forEach(doc => {
                data.push(
                    { 
                        companyName: doc.data().companyName,
                        endTime: doc.data().endTime,
                        eventAddress: doc.data().eventAddress,
                        eventDescription: doc.data().eventDescription,
                        startTime: doc.data().startTime,
                        startDay: doc.data().startDay,
                        timestamp: doc.data().timestamp,
                        username: doc.data().username,
                        venueName: doc.data().venueName,
                        imageLink: doc.data().imageLink,
                        startMonth: doc.data().startMonth,
                        startDate: doc.data().startDate,
                        title: doc.data().title,
                        cityName: doc.data().cityName,
                        videoLink: doc.data().videoLink,
                    })
            })
            setPostsList(data);
        }, (error) => {
            console.log(`PostAPI error: ${error}`)
        });
    }

I tried passing in the array as a whole thinking the function might compare the two but was incorrect.我尝试将数组作为一个整体传入,认为 function 可能会将两者进行比较,但这是不正确的。 Trying to figure out the best way to solve this problem and if I have to restructure my code.试图找出解决此问题的最佳方法,以及是否必须重组我的代码。 I know you can only have up to 10 where()'s but I don't think Ill need to do that, nor do I think that would be the most efficient solution.我知道你最多只能有 10 个 where(),但我认为我不需要这样做,我也不认为这是最有效的解决方案。

It sounds like you're looking for array-contains-any from the array query operators :听起来您正在从数组查询运算符中寻找array-contains-any

query(
  dbRef, 
  where("cityName", "==", selectedCity), 
  where("category", "array-contains-any", events), // 👈
  orderBy("timestamp", "desc")
)

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

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