如何使用Firestore在快照上订购价值 [英] How to orderByValue onSnapshot using Firestore
本文介绍了如何使用Firestore在快照上订购价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
componentWillMount
我注册了一个更新状态的onSnapshot函数。<$ c (doc)=> {
let todos = []
doc.forEach(doc => {todos.push(doc。 data())})
this.setState({
todos
})
})
}
pre>
然而,firebase / firestore工作的方式是只是推动随机密钥,所以当我得到数据时,它的顺序不正确。 p>
我知道有一个
.orderByValue()
函数,但是我试过实现它,它出。解决方案我的ref是
const todoRef = db.collection(todos); / code>
因此,一旦您获得了对集合的引用,就可以执行排序查询。 b $ b
todoRef.orderBy('createdAt')。onSnapshot((docSnapShot)=> {
let todos = []
docSnapShot.forEach(doc => {todos.push(doc.data())})
this.setState({
todos
})
})
In
componentWillMount
I am registering an onSnapshot function which updates the state.componentWillMount () { todoRef.onSnapshot((doc) => { let todos = [] doc.forEach(doc => {todos.push(doc.data())}) this.setState({ todos }) }) }
However, the way that firebase/firestore works is that it just pushes up random keys, so when I get the data back it's not in the correct order.
I know there is a
.orderByValue()
function but I've tried implementing it and can't seem to figure it out.解决方案My ref was
const todoRef = db.collection("todos");
So once you've got the reference to your collection, you can then do the sort query.
todoRef.orderBy('createdAt').onSnapshot((docSnapShot) => { let todos = [] docSnapShot.forEach(doc => {todos.push(doc.data())}) this.setState({ todos }) })
这篇关于如何使用Firestore在快照上订购价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文