Skip to content

bug(query): queries with a collection & subcollection but an empty doc fail silently #269

Open
@puppybits

Description

What is the current behavior?
If a query string has a collection and subcollection but not a docId it's an invalid query. No error is getting thrown.

What is the expected behavior?
In the queryStrToObj function, if there is a collection and subcollection, there must always be a doc. I assume an error would be thrown just like in the getQueryConfig function below it.

throw new Error(
'Invalid Path Definition: Only Strings and Objects are accepted.',
);

Which version of redux-firestore are you using? What about other dependencies?
v0.12.0

Which environments/browsers are affected by this issue? Did this work in previous versions or setups?
all

Minimal demo to reproduce issue (using codesandbox or similar)

let docId = ''
getQueryConfigs(`/test/${docId}/col/subId`); // returns object with missing doc param {collection:'test', subcollection:[{collection:'col', doc:'subId'}]'
// or
useFirestoreConnect({collection:`/test/${docId}/col/subId`})

I would assume there to be a check in the queryStrToObj and a corresponding unit test to validate.

export function queryStrToObj(queryPathStr, parsedPath) {
const pathArr = parsedPath || trim(queryPathStr, ['/']).split('/');
const [collection, doc, ...subcollections] = pathArr;
const queryObj = {};
if (collection) queryObj.collection = collection;
if (doc) queryObj.doc = doc;
if (subcollections.length) {
queryObj.subcollections = [queryStrToObj('', subcollections)];
}
return queryObj;
}

A check could be something like:

if (collection && subcollection && doc === nil) {
   throw new Error(
    'Invalid Path Definition: Parent document path is empty.',
  )
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions