Firebase Invalid document reference. Document references must have an even number of segments

前端 未结 3 1115
生来不讨喜
生来不讨喜 2021-01-18 05:57

What is wrong with this query?

const db = firebase.firestore()
const query = db.doc(this.props.user.uid).collection(\'statements\').orderBy(\'uploadedOn\',          


        
相关标签:
3条回答
  • 2021-01-18 06:40

    If you want to get a collection of documents with querying, you don’t have to specify a document id. Below code should work in this case.

    const query = db.collection('statements').orderBy('uploadedOn', 'desc').limit(50)
    

    Or if you want to get the document, you can pass the document id to doc() method. In that case, the code should be.

    const query = db.collection('statements').doc(this.props.user.uid)
    

    For more details about querying firestorm data: https://firebase.google.com/docs/firestore/query-data/get-data?authuser=0

    0 讨论(0)
  • 2021-01-18 06:52

    For others having this issue, make sure that no document reference has an empty string.

    I had this issue when using a get method with uid input as below and forgot to check if uid is empty

    private fun getFullRef(uid: String): CollectionReference {
            return ref.document(uid).collection(FireContact.SUB_PATH)
        }
    
    0 讨论(0)
  • 2021-01-18 06:55

    Since you haven't described what exactly you're trying to query, I'll just point out that all documents must be in a collection, without exception. So, if you say this:

    db.doc(this.props.user.uid)
    

    Firestore assumes that the string you're passing to doc() contains both the collection and document id separated by a slash. But this seems to be highly unlikely in your case. You need to determine which collection the uid is in, and use that first when you build the reference to the collection you want to query. Assuming that you do have a statements subcollection in the uid document, and that some other collection contains the uid document, you'll have to specify the full path like this:

    db.collection('that-other-collection').doc(this.props.user.uid).collection('statements')
    

    Of course, only you know the actual structure of your data.

    0 讨论(0)
提交回复
热议问题