Skip to content

CollectionReference.doc function path should be optional #1974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Omripresent opened this issue Dec 15, 2018 · 13 comments
Closed

CollectionReference.doc function path should be optional #1974

Omripresent opened this issue Dec 15, 2018 · 13 comments

Comments

@Omripresent
Copy link

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

@codediodeio
Copy link
Contributor

codediodeio commented Dec 19, 2018

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

@yuliankarapetkov
Copy link

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

@rmolinamir
Copy link

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.

@JerryBels
Copy link

+1

@cwborion
Copy link

cwborion commented Feb 8, 2019

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.

@rmolinamir
Copy link

If you’re getting this error when trying to delete, I think you might be sending an undefined id.

@cwborion
Copy link

cwborion commented Feb 8, 2019

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

@mattrmaydew
Copy link

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

@gildniy
Copy link

gildniy commented May 24, 2019

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

@jamesdaniels
Copy link
Member

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

@schankam
Copy link

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....

@Julieng50
Copy link

Julieng50 commented Dec 6, 2019

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

@elongvieira
Copy link

It is working for me:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests