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
Firestore: How to check if document exists, and create one if it doesn't #1272
Comments
Don't think you can do it with AFS, and might have to rely on the underlying framework:
|
this.afs.doc('/path').take(1).do(d => d.payload.exists()) As of 5.0 rc3 empty sets/docs should be handled correctly. I am working on an API to make return or create easier. |
@jamesdaniels I import the operator take already but i got error |
I'm doing it manually - hopefully there is a better way? this.dataDoc = this.myDataCollectionRef.doc(id);
this.data = this.dataDoc.snapshotChanges().map(action => {
if (action.payload.exists === false) {
return null;
} else {
const data = action.payload.data() as Whatever;
const id = action.payload.id;
return { id, ...data };;
}); Might be incorrect usage of snapshotChanges(), but its able to determine if something is there. If you wanted to manually create it, I believe you could do that once you've determined it doesn't exist. |
Just popping in to report that neither |
Currently handling this by attempting an this.afs.doc(`users/${uid}`)
.update({data})
.then(() => {
// update successful (document exists)
})
.catch((error) => {
// console.log('Error updating user', error); // (document does not exists)
this.afs.doc(`users/${result.uid}`)
.set({data});
}); |
@jamesdaniels example is missing somethings:
So,
|
i check simply , not sure if that is the right way to do .. const userRef: AngularFirestoreDocument = this.afs.doc( |
Here's a variation of @sneurlax's example: //Create an empty document, or do nothing if it exists.
this.afs.doc('users/sam').set({ }, { merge: true });
//Create a document with name property set to 'Sam'.
//If document already exists, it will still set the name property to 'Sam'
this.afs.doc('users/sam').set({ name: 'Sam' }, { merge: true }); |
@buchmiller |
How about searching 2 parameters, like username and email? I think it could be in 2 ways:
|
|
You can use
|
Hello, I am using firestore from firebase(npm) and the property https://firebase.google.com/docs/firestore/query-data/get-data
querySnapshot in that context have a property |
@mk4yukkalab do you got workaround to ur concern And if i need a query like : this.afs.collection('users', ref => ref.where('email', '==', email)) how i can do that? |
do you know how do that ?? |
do you know how do that ?? |
firestore This one should work |
@mk4yukkalab @Hanan0o0 @Vino16491
|
Trying to do something similar in iOS. I think this should be done in a transaction though because there's plenty of time for data to be written with the ID between the |
@larssn solution still works as of date. |
Are you sure? For me it makes more sense, not be a cost in case of update. So, the cost will be the same as @sneurlax method. |
For Android in Kotlin:
|
I am not an expert so there may be better ways, but here is what I did in that situation: |
Maybe a bit late, but this worked for me for a web app.
|
The way I went about this a combo of creating my own unique ID based on the data by using a sha library (js-sha256), just JSON.stringify(data) to get your id then follow the approach above.
Data is always overwritten. |
.then is not applicable after .get or valuechanges |
I am also trying to find a solution for angular 8 |
All these code examples which use "docSnapshot.exists" checking, arent they forgetting that this is not a transaction, so it can not guarantee that by the time the client find out that doc does not exist and it triggers a create new doc, someone else might have created that doc, in between get() and set() calls ? (although its probability is less but not zero) |
Thought Id share this...I am doing it like so:
any objections? If the doc exists it uses |
yes but in this case, the previous record of sam would override how to handle that? |
but in this case previous recod would be override with the new one right? how can i check if previous was present don't write new one. or on't update new values |
how to check if doc exist show an toast that doc exist and if not set the value? |
Am So happy It Working. Thank You |
It looks like you need import { AngularFirestore } from '@angular/fire/compat/firestore';
constructor(public db: AngularFirestore){};
const collectionRef = this.db.collection('my-collection').doc('my-document').collection('sub-collection');
collectionRef.doc('abc').ref.get().then(snapshot => {
if (snapshot.exists) {
console.log('document abc exists');
} else {
console.log('document abc DOES NOT exist');
}
}); |
*AngularFireStore:
Is there a way to check if a document already exists in the database?
Couldn't find an example.
Thank you
The text was updated successfully, but these errors were encountered: