how to stop slash from splitting the firebase database child

Advertisements

I am working on an App that allows the student to register using their registration number. the registration number is like 17/csc/001 till infinity. The student registration number will saved as a child of that firebase reference but the issue I am having is that firebase splits the registration number into three places due to the slash that’s found there. I need help on how to resolve it because there’s no how a student’s registration number will be without the slash.
I need something like this "Registration Numbers","17/csc/001" but I am having this

"Registration Numbers": {
    "17": {
      "CSC": {
        "001": {
          "registrationNumber": "17/CSC/001"
        }
      }
    }
  }

void addRegistrationNumber(){
        progressBar.setVisibility(View.VISIBLE);
        String regNumber = editText.getText().toString();
        final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.child("Registration Numbers").child(regNumber).exists()){
                    progressBar.setVisibility(View.GONE);
                    showMessage("Error","You have Already Added this Registration Number");
                }else {
                    HashMap<String, Object> hashMap = new HashMap<>();
                    hashMap.put("registrationNumber",regNumber);
                    databaseReference.child("Registration Numbers").child(regNumber).updateChildren(hashMap)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()){
                                        progressBar.setVisibility(View.GONE);
                                        Toast.makeText(AdminAddOrRemoveARegistrationNumberActivity.this, "Registration Number Added Successfully", Toast.LENGTH_SHORT).show();
                                    }else {
                                        progressBar.setVisibility(View.GONE);
                                        Toast.makeText(AdminAddOrRemoveARegistrationNumberActivity.this, "Error Occurred, Please try Again", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                progressBar.setVisibility(View.GONE);
                Toast.makeText(AdminAddOrRemoveARegistrationNumberActivity.this, "Database error "+error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

>Solution :

The / character is not allowed to be present in keys in the Firebase Realtime Database. So there’s no way for you to include them in the keys.

What you’ll want to do instead is to encode the / values in your student registration numbers, with a value that can’t occur in those numbers but is allowed in the database keys. For example, if _ can’t occur in your student registration numbers, then you could encode 17/csc/001 as 17_csc_001.

Leave a ReplyCancel reply