[英]How to get all documents in Firebase collection with specific value?
[英]How do I query a collection of firebase documents by a properties value?
我正在構建一個帶有搜索欄的 Firebase React 社交媒體應用程序,我希望我的搜索欄根據輸入的值顯示用戶的建議。 如果我輸入“F”,我希望是 Foo,然后在 Bar 下方,這與過濾用戶並返回相關用戶的任何社交媒體搜索欄非常相似。 . 我無法理解 Firebase 查詢以及適用於此的查詢。
數據庫的布局是用戶 collections 文檔,代表用戶文檔上的用戶名屬性
const searchUser = async (text) => {
const queryUsers = [];
if (text !== '') {
try {
const usersRef = collection(firestore, "users");
const q = query(usersRef, orderBy("username"),startAt(text.toLowerCase()),limit(5))
const querySnapshot = await getDocs(q)
querySnapshot.forEach((doc) => {
queryUsers.push(doc.data())
})
} catch (error) {
console.error(error);
}
}
console.log(queryUsers);
return queryUsers;
};
我嘗試了各種查詢,但沒有一個有效,我希望讓所有用戶按發送到查詢的字符串的值排序
發現問題,用戶名是用大寫字母保存的。 不確定這個查詢是否也是最好的查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.