8
votes

What is wrong with this query?

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

I get the following error:

Uncaught Error: Invalid document reference. Document references must have an even number of segments, but FrMd6Wqch8XJm32HihF14tl6Wui2 has 1
    at new FirestoreError (index.cjs.js:346)
    at Function.DocumentReference.forPath (index.cjs.js:15563)
    at Firestore.doc (index.cjs.js:15368)
    at UploadStatementPresentation.componentWillMount (UploadStatementPage.jsx:61)
    at UploadStatementPresentation.componentWillMount (createPrototypeProxy.js:44)
    at callComponentWillMount (react-dom.development.js:6872)
    at mountClassInstance (react-dom.development.js:6968)
    at updateClassComponent (react-dom.development.js:8337)
    at beginWork (react-dom.development.js:8982)
    at performUnitOfWork (react-dom.development.js:11814)
3
Please describe your database layout with the collections and documents you intend to access with this query.Doug Stevenson

3 Answers

24
votes

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
votes

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
votes

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)
    }