add data to firebase firestore 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: javascript write to firebase
function writeUserData(userId, name, email, imageUrl) {
firebase.database().ref('users/' + userId).set({
username: name,
email: email,
profile_picture : imageUrl
});
}
Example 3: android java add new record to Firebase Firestore
Map<String, Object> city = new HashMap<>();
city.put("name", "Los Angeles");
city.put("state", "CA");
city.put("country", "USA");
db.collection("cities").document("LA")
.set(city)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});