Conditional where clause in firestore queries

后端 未结 1 1221
不知归路
不知归路 2020-11-28 13:58

I have fetch some data from firestore but in my query I want to add a conditional where clause. I am using async-await for api and not sure how to add a consitional where cl

相关标签:
1条回答
  • 2020-11-28 14:47

    Add the where clause to the query only when needed:

    export async function getMyPosts (type) {
      await api
      var myPosts = []
    
      var query = api.firestore().collection('posts')
      if (your_condition_is_true) {  // you decide
        query = query.where('status', '==', 'published')
      }
      const questions = await query.get()
        .then(snapshot => {
          snapshot.forEach(doc => {
            console.log(doc.data())
          })
        })
        .catch(catchError)
    }
    
    0 讨论(0)
提交回复
热议问题