i am using Firebase and Node.
I would like to use the same method to either update an object, or create it, if for some reason it does not exist.
Consider the following method
const firebaseSave = async function(data) {
const uid = firebase.auth().currentUser.uid
const profilePath = `users/${uid}`
const profileData = {
name: data.name,
}
const userRef = firebaseDb.child(profilePath)
await userRef.set(profileData)
}
What would be the best and correct way to determine if update or set should be called?
thanks
i am using Firebase and Node.
I would like to use the same method to either update an object, or create it, if for some reason it does not exist.
Consider the following method
const firebaseSave = async function(data) {
const uid = firebase.auth().currentUser.uid
const profilePath = `users/${uid}`
const profileData = {
name: data.name,
}
const userRef = firebaseDb.child(profilePath)
await userRef.set(profileData)
}
What would be the best and correct way to determine if update or set should be called?
thanks
Share Improve this question edited Feb 24, 2017 at 20:21 Lee asked Feb 24, 2017 at 17:08 LeeLee 5,9366 gold badges46 silver badges63 bronze badges 3-
1
If you want to replace all the data at
userRef
then you call set(). If you only want to update some fields, and leave others unchanged, you use update(). – Kato Commented Feb 24, 2017 at 22:50 - I understand the set and update use cases, but is there a way to decide if to use update, if the record already exists. – Lee Commented Feb 26, 2017 at 10:13
- 3 It doesn't have to exist to call update(). Give it a try. – Kato Commented Feb 27, 2017 at 23:27
4 Answers
Reset to default 5basically with:
"set" you write or replace data to a defined path, like messages/users/
, you can update the information or create it.
Check this out: https://firebase.google./docs/database/admin/save-data
I'd say get the data, check if there was something, if there wasn't default to empty object - then update that object.
Something like:
const valueSnapshot = await userRef.once('value');
const userValue = valueShapshot.exists() ? valueShapshot.val() : {};
const profileData = { ...userValue, name: data.name };
await userRef.set(profileData);
That is, assuming you would want to keep the existing data, and merge any new data into it. If you don't care about overwriting anything, no check would be needed at all.
await userRef.set(profileData, {merge: true})
You can add {merge: true} which will update the previous document and will create a new document if document do not already exist.
This is my idea, I apply it for the frontend side.
I use the id to identify if we should create or update.
Because on the frontend, usually, new data don't have any id.
In this way, we don't have to check the doc exists or not.
I'm not sure about backend, but it's okay on the frontend.
createOrUpdateTemplate(template: Label): Observable<unknown> {
if (!template.id) {
return from(
this.fs.collection<Label>('templates').add({
...template,
createdAt: firebase.default.firestore.FieldValue.serverTimestamp(),
updatedAt: firebase.default.firestore.FieldValue.serverTimestamp(),
})
);
} else {
return from(
this.fs
.collection<Label>('templates')
.doc(template.id)
.update({
...template,
updatedAt: firebase.default.firestore.FieldValue.serverTimestamp(),
})
);
}
}