我從一個特定的集合中獲取,它有 100 多個值,我想按等級對它們進行排序。我有一個排名欄位。我在下面的代碼中將訂單和限制放在哪里。
useEffect(
() =>
onSnapshot(collection(db, "collectiondb/id/users"), (snapshot) =>
setLeaders(snapshot.docs.map((doc) => ({ ...doc.data(), id: doc.id })))
),
[]
);
這是它的輸出方式。
const [leaders, setLeaders] = useState([{ name: "Loading...", id: "initial" }]);
<ul type='1'>
{leaders.map((leader) => (
<li>
{leader.rank} {leader.name} </>
</li>
))}
</ul>
uj5u.com熱心網友回復:
你可以這樣使用.orderBy:
collection(db, 'collectiondb/id/users')
.orderBy('rank')
.orderBy('createdDate', 'desc')
.limit(100)
編輯:OP的解決方案:
useEffect(() => {
const collectionRef = collection(db, 'collectiondb/id/users');
const q = query(collectionRef, limit(10), where('rank', '>', 0), orderBy('rank', 'asc'));
const unsub = onSnapshot(q, (snapshot) => setLeaders(snapshot.docs.map((doc) => ({ ...doc.data(),
id: doc.id
}))));
return unsub;
}, []);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/526338.html
標籤:Google Cloud Collective javascript反应火力基地谷歌云火库
