How do I check if a document exists in Firebase Firestore, if not create a new one.
this is my code for now:
create(doc: any, collection: string): any {
this.db.collection(collection, ref => ref.where('nome', '==', doc.nome))
.snapshotChanges()
.subscribe((ref: any) => {
console.log('Ref = ', ref);
if (ref.length <= 0) {
this.db.collection(collection).add({ ...doc }).then(
res => console.log('Inserido com sucesso : ', res)
).catch(err => console.log('Erro : ', err));
}
else {
console.log('Evento ja existe : ', ref);
}
}).unsubscribe();
}