Google Firestore - how to get document by multiple ids in one round trip?

前端 未结 11 1793
余生分开走
余生分开走 2020-11-21 22:49

I am wondering if it\'s possible to get multiple documents by list of ids in one round trip (network call) to the Firestore.

11条回答
  •  北海茫月
    2020-11-21 23:38

    In practise you would use firestore.getAll like this

    async getUsers({userIds}) {
        const refs = userIds.map(id => this.firestore.doc(`users/${id}`))
        const users = await this.firestore.getAll(...refs)
        console.log(users.map(doc => doc.data()))
    }
    

    or with promise syntax

    getUsers({userIds}) {
        const refs = userIds.map(id => this.firestore.doc(`users/${id}`))
        this.firestore.getAll(...refs).then(users => console.log(users.map(doc => doc.data())))
    }
    

提交回复
热议问题