Angularfire: Cannot create document reference with an auto-generated ID

Created on 18 Feb 2018  路  3Comments  路  Source: angular/angularfire

Version info

Angular:
"@angular/core": "^5.2.1",

Firebase:
CloudFirestore

AngularFire:
angularfire2 : "^5.0.0-rc.6"

Issue

I am unable to get a document reference with an auto generated id. Base on the cloudFirestore documentation it should be possible to do the following.

var newCityRef = db.collection('cities').doc();
// Later...
var setDoc = newCityRef.set({
    // ...
});

Currently, the the angularfire2 collection's doc() function has a mandatory parameter hence calling angularFirestore.collection('cities').doc() results in a typescript error.

Is this something the team is aware of? Are there plans to fix it?
Thanks

P.S
I know I can do collection angularFirestore.collection('cities').set({...}) which would auto generate a document id. However, I am trying to do a batch job which requires a document ref and my document is not pre-existing. Example

const batch = angularFirestore.firestore.batch();

const articleDocRef = angularFirestore.collection('articles').doc();
const journalistDocRef = angularFirestore.collection('journalist').doc('mike');

batch.set(articleDocRef, {'title': 'amazing article'});
batch.update(journalistDocRef, {'totalArticles': 200});
batch.commit()

In order for the above to work I have to get a docRef with an auto generated ID which is currently not possible.

Most helpful comment

Use AngularFirestore's createId() method for this.

All 3 comments

Use AngularFirestore's createId() method for this.

thanks

Use AngularFirestore's createId() method for this.

Ok but how do we use the createId() method?
Can you share an example?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

goekaypamuk picture goekaypamuk  路  3Comments

jnupeter picture jnupeter  路  3Comments

jteplitz picture jteplitz  路  3Comments

DennisSmolek picture DennisSmolek  路  3Comments

martinyoussef picture martinyoussef  路  3Comments