1
我正在使用Firebase和節點。Firebase:更新或創建
我想用同樣的方法更新一個對象,或者創建它,如果由於某種原因它不存在。
考慮以下方法
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)
}
什麼會,以確定是否更新或集應該叫最好的,正確的方法是什麼?
感謝
如果你想要替換'userRef'處的所有數據,那麼你可以調用set()。如果您只想更新某些字段,並保持其他字段不變,則使用update()。 – Kato
我理解設置和更新用例,但是如果記錄已經存在,是否有方法來決定是否使用更新。 – Lee
它不一定存在才能調用update()。試一試。 – Kato