Skip to content

Commit

Permalink
fix: Better error for Collection Group Queries with documentId() curs…
Browse files Browse the repository at this point in the history
…ors (#720)
  • Loading branch information
schmidt-sebastian committed Aug 1, 2019
1 parent 52b3130 commit 169286d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
19 changes: 19 additions & 0 deletions dev/src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,25 @@ export class Query {
let reference: DocumentReference;

if (typeof val === 'string') {
const path = basePath.append(val);

if (this._queryOptions.allDescendants) {
if (!path.isDocument) {
throw new Error(
'When querying a collection group and ordering by ' +
'FieldPath.documentId(), the corresponding value must result in ' +
`a valid document path, but '${val}' is not because it ` +
'contains an odd number of segments.'
);
}
} else if (val.indexOf('/') !== -1) {
throw new Error(
'When querying a collection and ordering by FieldPath.documentId(), ' +
`the corresponding value must be a plain document ID, but '${val}' ` +
'contains a slash.'
);
}

reference = new DocumentReference(this._firestore, basePath.append(val));
} else if (val instanceof DocumentReference) {
reference = val;
Expand Down
19 changes: 18 additions & 1 deletion dev/test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,9 @@ describe('startAt() interface', () => {
expect(() => {
query.orderBy(FieldPath.documentId()).startAt('doc/coll');
}).to.throw(
'Only a direct child can be used as a query boundary. Found: "coll/doc/coll/doc/coll".'
'When querying a collection and ordering by FieldPath.documentId(), ' +
'the corresponding value must be a plain document ID, but ' +
"'doc/coll' contains a slash."
);
});

Expand Down Expand Up @@ -1867,4 +1869,19 @@ describe('collectionGroup queries', () => {
);
});
});

it('rejects slashes', () => {
return createInstance().then(firestore => {
const query = firestore.collectionGroup('collectionId');

expect(() => {
query.orderBy(FieldPath.documentId()).startAt('coll');
}).to.throw(
'When querying a collection group and ordering by ' +
'FieldPath.documentId(), the corresponding value must result in a ' +
"valid document path, but 'coll' is not because it contains an odd " +
'number of segments.'
);
});
});
});

0 comments on commit 169286d

Please sign in to comment.