Firebase查询返回随机用户数据而不是过滤后的用户数据。

huangapple go评论56阅读模式
英文:

Firebase query returns random user data instead of filtered user data

问题

我正在尝试从数据中的“users”集合中提取数据,其中用户的名称与搜索查询匹配/部分匹配。我的用户文档现在只有3个字段,1.名称 2.电子邮件 3.uid。这是我第一次使用Firebase。非常感谢帮助。以下是代码片段:

const results: TeamMemberData[] = [];
const usersRef = collection(db, "users");
const q = query(usersRef, where("name", ">=", searchQuery));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
  console.log(doc.data()); // 这会记录随机用户数据

  if (currentUser?.email !== doc.data().email)
    results.push({
      name: doc.data().name,
      email: doc.data().email,
      uid: doc.data().uid,
    });
});

我在这里记录了结果
我期望结果数组只包含那些在Firestore文档中名称匹配的用户。我只输入了“g”,但我得到了四个用户,其中三个甚至在其名称属性中都没有“g”。

英文:

Im trying to fetch data from 'users' collection from data, where the name of user matches/partially matches the searchQuery. My user document has only 3 fields now, 1.name 2.email 3.uid. Im working with firebase for the first time. Help is much appreciated. Here is the code snippet:

const results: TeamMemberData[] = [];
        const usersRef = collection(db, "users");
        const q = query(usersRef, where("name", ">=", searchQuery));
        const querySnaphot = await getDocs(q);
          querySnaphot.forEach((doc) => {
            console.log(doc.data()); // this logs random user data 

            if (currentUser?.email !== doc.data().email)
              results.push({
                name: doc.data().name,
                email: doc.data().email,
                uid: doc.data().uid,
              });
          });

im logging the results here
I expected to get the results array would have only those users whose name matched in document in firestore.I only types 'g' but im getting four users, where, three of them doesn't even have 'g' in their name property.

答案1

得分: 1

你之所以得到随机用户,是因为你正在使用 ">=" 作为查询运算符,请查看这里以了解在与字符串一起使用时的工作原理。


在你的示例中,你需要使用 == 运算符:

const q = query(usersRef, where("name", "==", searchQuery));

或者也可以使用array-contains运算符:

const q = query(usersRef, where("name", "array-contains", searchQuery));

这个查询返回所有名字字段是包含 searchQuery 的数组的用户文档。如果数组中有多个与你查询的值匹配的实例,该文档只会在结果中包含一次。

英文:

you got random users because you are using &quot;&gt;=&quot; as Query operators, have a look here how this works when used with strings.<br>


In your example you need to use the == operator:

const q = query(usersRef, where(&quot;name&quot;, &quot;==&quot;, searchQuery));

or maybe array-contains operator:

const q = query(usersRef, where(&quot;name&quot;, &quot;array-contains&quot;, searchQuery));

>This query returns every user document where the name field is an array that contains searchQuery. If the array has multiple instances of the value you query on, the document is included in the results only once.

答案2

得分: 0

You can also try "in" as includes like:

const q = query(usersRef, where("name", "in", [searchQuery]));
英文:

u can also try "in" as includes like :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 const q = query(usersRef, where(&quot;name&quot;, &quot;in&quot;, [searchQuery]));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 14:26:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定