add method in firebase firestore code example
Example 1: firebase functions add to database
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
exports.onMsgAdded = functions.database.ref('/users/{uid}/messages/{to_uid}/{msg}').onCreate((snap, context) =>{
const data = snap.val();
const msgRoot = admin.database().ref('/users/' + context.params.to_uid + '/messages/' + context.params.uid);
return msgRoot.set(data);
});
Example 2: 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);
}
});