按最新优先顺序订购消防仓库文档



所以基本上我添加这样的文档:

db.collection('posts').add({
name: usernameFinal,
photoUrl: photoFinal,
text: caption,
imageUrl: imageUrl,
isPost: true

});

然后我映射所有文档。

{posts.map(({id, post}) => {
if(post.isPost == true){
return <Post
key={id}
id={id}
name={post.name}
photoUrl={post.photoUrl}
text={post.text}
imageUrl={post.imageUrl}



/>
}

}

但当我绘制它的地图时,它并不整齐。那么,我如何按顺序添加文档或按顺序映射文档呢?

Firestore没有任何关于文档顺序/时间的隐含数据,因此为了允许您描述的顺序,您必须在每个文档中都有一个Timestamp字段。

一旦你有了这样的字段(比如createdAt(,你就可以在您可以通过调用来获取数据

db.collection('posts').orderBy('createdAt', 'desc')

最新更新