ADD DATA TO FIREBASE code example
Example 1: firestore set a document
let data = {
name: 'Los Angeles',
state: 'CA',
country: 'USA'
};
let setDoc = db.collection('cities').doc('LA').set(data);
Example 2: add to firebase database
db.collection("cities").add({
name: "Tokyo",
country: "Japan"
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
Example 3: javascript write to firebase
function writeUserData(userId, name, email, imageUrl) {
firebase.database().ref('users/' + userId).set({
username: name,
email: email,
profile_picture : imageUrl
});
}
Example 4: firebase set vs update
firebase.database().ref().child('/posts/' + newPostKey)
.set({ title: "New title", body: "This is the new body" });
Example 5: firebase update data
await firebase.database().ref().update(updates);