Angularfire: CollectionReference.doc function path should be optional

Created on 15 Dec 2018  路  13Comments  路  Source: angular/angularfire

Version info

Angular: 7.1.2

Firebase: 5.6.0

AngularFire: 5.1.1

Other (e.g. Ionic/Cordova, Node, browser, operating system):

How to reproduce these conditions

Steps to set up and reproduce

Example code:

const ref = this.afs.collection(this.collectionRef).doc();

Failing on the following error:

Expected 1 arguments, but got 0.ts(2554)
collection.d.ts(18, 12): An argument for 'path' was not provided.

But should be valid per firebase documentation and source:
Add doc example https://firebase.google.com/docs/firestore/manage-data/add-data
Source definition of doc function https://github.com/firebase/firebase-js-sdk/blob/master/packages/firebase/index.d.ts#L1951

Debug output

* Errors in the JavaScript console *

ERROR Error: Uncaught (in promise): FirebaseError: [code=invalid-argument]: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined
FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined

Expected behavior

Doc reference should be created per firebase documentation

Actual behavior

Invalid argument error thrown

Fix

path argument should be optional in https://github.com/angular/angularfire2/blob/master/src/firestore/collection/collection.ts#L141

Most helpful comment

I agree, this should align with the Firebase SDK. A potential workaround is to use a pattern like this:

const id = this.afs.createId();
const ref = this.afs.collection(this.collectionRef).doc(id);

All 13 comments

I agree, this should align with the Firebase SDK. A potential workaround is to use a pattern like this:

const id = this.afs.createId();
const ref = this.afs.collection(this.collectionRef).doc(id);

+1 Is this going to be fixed in future releases?

Ran into this earlier as well, had to pass a randomly generated string using date constructors to force creation in the database as a workaround, but was totally caught off guard by this error thrown since it contradicts the docs.

+1

I am getting this error as well, when trying to delete a document, however, it is working in another place in the app, so I am extra confused.

If you鈥檙e getting this error when trying to delete, I think you might be sending an undefined id.

ah yes you were right! Thank you! Because of the type and positioning of the component I needed to access the id differently

undefined id. doc['.key'] wasn't working for me any longer had to go doc.id

What if one does this: .doc('') instead?!
Me I got this error claiming to have been undefined then I first checked if the doc id exists then I passed that line query inside the if statement on truthy. if(id.lenght) ....doc(id);

This will be addressed in 5.2, tossed it in real quick as c2354f8. #2086

Mhh, just updated to @angular/fire 5.2 and firebase 6.2.1, and the problem is still here for me.

I'm getting the following error:

CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined

when trying to set a batch write operation calling "set", with this.afs.collection<Offer>('offers').doc().ref .

The solution given here works for me... But calling an empty doc on a collection works well with the firebase-admin SDK to create new documents (in case of a batch write using "set"for example), I was expecting the same with the JS client....

Here is the bug (line 5) :
````

  1. CollectionReference.prototype.doc = function (pathString) {
  2. validateBetweenNumberOfArgs('CollectionReference.doc', arguments, 0, 1);
  3. // We allow omission of 'pathString' but explicitly prohibit passing in both
  4. // 'undefined' and 'null'.
  5. if (arguments.length === 0) { // Impossible because there is 1 argument, even if not defined (undefined)
  6. pathString = AutoId.newId();
  7. }
  8. validateArgType('CollectionReference.doc', 'non-empty string', 1, pathString);
  9. if (pathString === '') {
  10. throw new FirestoreError(Code.INVALID_ARGUMENT, 'Document path must be a non-empty string');
  11. }
  12. var path = ResourcePath.fromString(pathString);
  13. return DocumentReference.forPath(this._query.path.child(path), this.firestore);
    14.};
    ````

Workaround :
var id = this.firestore.createId(); var doc = this.firestore.collection('myCollection').doc(id);

It is working for me:

this.db.collection(collectionn).add(obj);
or
this.db.collection(this.collectionMain).doc(this.db.createId()).set(obj)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

harrylincoln picture harrylincoln  路  3Comments

Leanvitale picture Leanvitale  路  3Comments

jteplitz picture jteplitz  路  3Comments

avanderbergh picture avanderbergh  路  3Comments

jnupeter picture jnupeter  路  3Comments