Firebase data update - java

I want to change the user role.
I have a collection of users with the user id. Each user has fields with mail, name, role, etc.
DatabaseReference reference;
reference = FirebaseDatabase.getInstance().getReference("users");
reference.child(document.getId()).child("Role").setValue("3");
document.getId() shows the correct user id but nothing changes in the database
Database screen

Ok it was stupid mistake. I'm working on Cloud Firestore and I've tried to change to
Realtime Database
Correct
DocumentReference docfer=db.collection("users").document(document.getId());
Map<String,Object> map = new HashMap<>();
map.put("Role","3");
docfer.update(map)

Your screenshot displays a Firestore database and not a Firebase Realtime Database. Both are apart of Firebase services but are actually two totally different products. So you need to use method that are apart of Firestore SDK. To change the value of the "Role" property, please use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
DocumentReference uidRef = usersRef.document(uid);
uidRef.update("Role", "3").addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "The document is updated successfully!");
}
}
});
The result will be the update of the "Role" property from "0" to "3".

Use "update" :
reference.child(document.getId()).child("Role").update("3");
than "setValue":
reference.child(document.getId()).child("Role").setValue("3");

Related

Data is not written into the Firebase Database from Android

I have set up a Firebase database that has 2 nodes. One node is called "ingredients" and the other is called "orders". I set up the nodes manually by using a JSON file and I just entered 1 entry in the node "orders" manually. You can see a screenshot of the structure of the database:
Now I would like to add further entries in the table "orders" (so basically I would like to add a further row).
In my Android application I use the following code:
// in the class definition of the Fragment
public DatabaseReference firebase_DB;
...
// in the oneCreate Method of the Fragment
firebase_DB = FirebaseDatabase.getInstance().getReference("orders");
...
// when clicking on a button in the Fragment which calls the onClick method
String id = firebase_DB.push().getKey();
DB_Object_Test currentOrder= new DB_Object_Test("testInfo_2", "testName", 2);
firebase_DB.child(id).setValue(currentOrder);
So basically the code runs without an error and the firebase_DB.child(id).setValue(currentOrder); is called. However, nothing changes in the Firebase console. I can't see any new entry there. The firebase database is properly connected to the app (at least Android Studio says that) and I have not specified any rules that would prohibit writing something on the database. The same data is stored without any problems in an SQLite database.
Can any one of you think about a possible reason for this problem? Why is the new row not written into the firebase database? I'll appreciate every comment.
EDIT: Here is the adjusted code that writes to the firebase database (by using 3 different approaches) after clicking a "Order" Button in the Fragment.
public void onClick(View view) {
if(view.getId() == R.id.ordering_button) {
/* Firebase approach 1
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("");
DatabaseReference usersRef = ref.child("orders");
Map<String, DB_Object_Test> order = new HashMap<>();
order.put("order_2", new DB_Object_Test("1", "testInfo_2", "testName", "2"));
usersRef.setValue(order);
*/
/*
Firebase approach 3 (by Alex)
*/
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ordersRef = rootRef.child("orders");
Map<String, Object> order = new HashMap<>();
order.put("info", "No additional information");
order.put("name", "Another Test Order");
order.put("table", "15");
Log.e("LogTag", "Before write firebase");
ordersRef.child("order_2").setValue(order).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Log.e("LogTag", "During on Complete firebase");
if (task.isSuccessful()) {
Log.e("dbTAG", "Data successfully written.");
} else {
Log.e("dbTAG", task.getException().getMessage());
}
}
});
Log.e("LogTag", "Afer write firebase");
/* Firebase approach 2
DatabaseReference firebase_DB = FirebaseDatabase.getInstance().getReference("orders");
String id = firebase_DB.push().getKey();
DB_Object_Test currentOrder= new DB_Object_Test(id, "testInfo_2", "testName", "2");
firebase_DB.child(id).setValue(currentOrder);
//Option 2*
//firebase_DB.setValue(currentOrder);
Log.e("LogTag", "firebase_DB.child(id): " + firebase_DB.child(id));
*/
Navigation.findNavController(getView()).navigate(...);
}
}
To be able to add another object under your "orders" node, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ordersRef = rootRef.child("orders");
Map<String, Object> order = new HashMap<>();
order.put("info", "No additional information");
order.put("name", "Another Test Order");
order.put("table", "15");
ordersRef.child("order_2").setValue(order).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "Data successfully written.");
} else {
Log.d("TAG", task.getException().getMessage());
}
}
});
This code will work only if you set the proper security rules in your Firebase console:
{
"rules": {
".read": true,
".write": true
}
}
Remember to keep these rules only for testing purposes. Besides that, you can also attach a complete listener to see if something goes wrong.
If you want to use DatabaseReference#push() method, then you should use the following line of code:
ordersRef.push().setValue(order);
Edit:
According to your last comment:
Database location: Belgium (europe-west1)
You should check my answer from the following post and add the correct URL to the getInstance() method:
getInstance() doesn't work with other location than us-central1 in Realtime Database
In your particular case it should be:
DatabaseReference rootRef = FirebaseDatabase.getInstance("https://drink-server-db-default-rtdb.europe-west1.firebasedatabase.app").getReference();

Tried to add a new field of data to Firebase user when registering, but no update X method in FirebaseUser.class file

I am using the Realtime database, so the users are store in the database. I am attempting to implement a 'currentLocation' field for the FirebaseUser objects so that I can eventually create a list for each user of nearby users. I was already using a HashMap to input the data, so I just added hashmap.put('currentLocation', ''); to create an empty current location field that can be updated later.
Unfortunately though I cannot find a way to set/update the currentLocation field as there is no updateCurrentLocation() method in the FirebaseUser.class. How do I add this method to this read-only file? Is this even the best way to do this?
Code:
//Dashboard.java
usedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
//get current user
final FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
//Updates the firebase db user with their current location.
fUser.updateCurrentLocation(location);
//This method is not in the firebaseuser.class file but needs to be!
//fUser.updateCurrentLocation(location);
}
}
});
//RegisterActivity.java
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, dismiss dialog and start register activity
progressDialog.dismiss();
FirebaseUser user = mAuth.getCurrentUser();
//Get user email and uid from auth
String email = user.getEmail();
String uid = user.getUid();
//When user is registered store user info in firebase realtime database too
//using HashMap
HashMap<Object, String> hashMap = new HashMap<>();
//put info in hasmap
hashMap.put("email", email);
hashMap.put("uid", uid);
hashMap.put("name", ""); //will add later (e.g. edit profile)
hashMap.put("currentLocation", "");
hashMap.put("onlineStatus", "online"); //will add later (e.g. edit profile)
hashMap.put("typingTo", "noOne"); //will add later (e.g. edit profile)
hashMap.put("phone", ""); //will add later (e.g. edit profile)
hashMap.put("image", ""); //will add later (e.g. edit profile)
hashMap.put("cover", ""); //will add later (e.g. edit profile)
//firebase database instance
FirebaseDatabase database = FirebaseDatabase.getInstance();
//path to store user data named "Users"
DatabaseReference reference = database.getReference("Users");
//put data within hashmap in database
reference.child(uid).setValue(hashMap);
Toast.makeText(RegisterActivity.this, "Registered...\n"+user.getEmail(), Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this, DashboardActivity.class));
finish();
//FirebaseUser.class Bytecode
#NonNull
public Task<Void> updateEmail(#NonNull String email) {
Preconditions.checkNotEmpty(email);
return FirebaseAuth.getInstance(this.zza()).zzn(this, email);
}
#NonNull
public Task<Void> updatePassword(#NonNull String password) {
Preconditions.checkNotEmpty(password);
return FirebaseAuth.getInstance(this.zza()).zzo(this, password);
}
#NonNull
public Task<Void> updatePhoneNumber(#NonNull PhoneAuthCredential credential) {
return FirebaseAuth.getInstance(this.zza()).zzp(this, credential);
}
#NonNull
public Task<Void> updateProfile(#NonNull UserProfileChangeRequest request) {
Preconditions.checkNotNull(request);
return FirebaseAuth.getInstance(this.zza()).zzq(this, request);
}
FirebaseUser class, has only a few fields that are related to the authentication. If you need more details about your users, you should create a POJO class to store additional data.
This data can be stored either in Cloud Firestore or in the Realtime Database so it can be later used.
Please also note, the Realtime Database doesn't store null values. Firestore does.
To update the location of the current user, you can do:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference("Users");
reference.child(uid).child("currentLocation").setValue("the location");
Since you mention wanting to find nearby users, I recommend checking out: Query for nearby locations and https://github.com/firebase/geofire-android/

How to check if a certain value is there in a Firebase database

What I want is: to check if the user input value is available in the database. I have an EditText in my app, the user enters a phone number. When he clicks the button, the button requests to see in the database to see if the user input matches any record of a phone number in the database.
I haven't done anything um lost, but I have a screenshot of my database structure.
To check for example, if "+263782876566" already exists in the database, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference officeRef = rootRef.child("Users").child("Office");
Query queryByPhone = officeRef.orderByChild("phone").equalTo("+263782876566");
officeRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
#Override
public void onComplete(#NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot ds : task.getResult().getChildren()) {
String type = ds.child("type").getValue(String.class);
Log.d(TAG, type);
}
} else {
Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
So we are using a reference that points to Users -> Office and then we create a Query object to actually check within all children if the "phone" property holds a specific value. If we find some results, we simply log the type. In your particular example, the result in the logcat will be:
Office member

Firebase authentication - Display user info

I use the firebase realtime database to store user information.
Here is a picture of the database:
screenshot of the database
For example, how can I get the value "nombre_dons" according to the connected user to display it in the application ? I use android studio in java.
Thank you in advance for your help
Those parent strings of the user data in Users node in your database look like the uids. If they indeed are, then all you need to do is to retrieve data from those uids for the particular user.
What I am saying looks something like this in code:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
FirebaseAuth mAuth = FirebaseAuth.getInstance();
ref.child(mAuth.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.child("nombre_dons").getValue(String.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}
});
One flaw I can see in your database image is that nombre_dons child in second user in your Users node has capital N, which would be a problem, because Firebase is case sensitive. I'd advise you to keep it same as others.
Assuming the keys inside 'Users' in your database are Firebase Authentication UserId(s), you can follow these steps-
1. Get UID of the current user.
2. Fetch data for the same.
Using Javascript -
var uid = firebase.auth().currentUser.uid;
firebase.database().ref('/Users/' + uid).once('value').then(function(snapshot){
var nombre_dons = snapshot.val().nombre_dons;
});

In-app delete Firebase record

Hoping for help on a small issue:
I am trying to delete singular records that are stored in my Firebase database through my Android application.
At present, when clicking the delete button, all items are deleted, as opposed to the single record that I have clicked on.
For completeness, I have also included my updateMaintenance method, which works perfectly. As you will see in the updateMaintenance method, I have used user-based authentication in mAuth.getCurrentUser() - I want to have this for my deleteMaintenance method also.
I'm sure this is just a case of adding a small bit of code, but not too sure where to start. Thanks in advance!
private void deleteMaintenance(String maintenanceId) {
DatabaseReference drMaintenance = FirebaseDatabase.getInstance().getReference("maintenance").child(maintenanceId);
FirebaseUser user = mAuth.getCurrentUser();
String uid = user.getUid();
databaseMaintenance.child(uid).removeValue();
Toast.makeText(this, "Maintenance record has been deleted", Toast.LENGTH_LONG).show();
}
private boolean updateMaintenance(String title, String desc, String id, String primary, String secondary, String property) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("maintenance").child(id);
Maintenance maintenance = new Maintenance (id, title, desc, primary, secondary, property);
FirebaseUser user = mAuth.getCurrentUser();
String uid = user.getUid();
databaseMaintenance.child(uid).child(id).setValue(maintenance);
databaseReference.setValue(maintenance);
Toast.makeText(this, "Maintenance Updated Updated Successfully", Toast.LENGTH_LONG).show();
return true;
}
This is happening because when using the following line of code:
databaseMaintenance.child(uid).removeValue();
You are deleting everything that is beneath the maintenance -> uid node. So basically you are deleting the uid node.
To achieve what you want, you need to change the reference. So please change the above line of code with:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("maintenance").child(uid).child(maintenanceId).removeValue();

Categories

Resources