• 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I am currently writing a part of an application where a user scans a QR code which holds the value of the user UID. Upon scanning this, I want to create a new key in my Firebase Realtime database containing the information related to the user with that UID called userInformation. Below is a an example of the format I wish the values to be stored as:

enter image description here

Currently it is being stored without the userInfromations key, it is only being stored with the UID, like so: enter image description here

Here is the related code:

case FirebaseVisionBarcode.TYPE_TEXT:
            {
                final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
                userInformations = new ArrayList<>();
                DatabaseReference username = ref.child(item.getRawValue()).child("username");
                DatabaseReference dateOfBirth = ref.child(item.getRawValue()).child("dateOfBirth");

                username.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String username = dataSnapshot.getValue(String.class);
                        //Toast.makeText(getApplicationContext(),username, Toast.LENGTH_LONG).show();

                        //it will create a unique id and we will use it as the Primary Key for our UserInfo
                        String id1 = ref.push().getKey();

                        //creating an UserInfo Object
                        UserInformation userInformation = new UserInformation(id1, username);

                        //Saving the Artist
                        ref.child(id1).setValue(userInformation);

                        //displaying a success toast
                        Toast.makeText(getApplicationContext(), username + " has been added", Toast.LENGTH_LONG).show();
                    }

I realize also that by changing:

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

to:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("userInformations");

I am able to get the key named userInformations, but it no longer saves the value for the userInformationName like below:

enter image description here

At the moment I am just unsure how to structure my code to get both of these things.

    • So basically you want to change the name of your property from username to userInformationName, right?
      • 2
    • @AlexMamo the issue is that the values are being saved as the UID key rather than under "UserInfromations". I made some changes above to the first picture to attempt to resolve the ambiguity.
    • You say: "Currently it is being stored without the userInfromations key" but I see that key exists. userInfromations child does not exist in the second picture. If you don't need that puushed key, remove the push() call, right?
      • 1
    • @AlexMamo I would like the unique ID key if possible like in the 3rd picture. Is the push() call necessary to do this? Removing push() will mean that the value for userInformationId is just "userInformations" rather than a unique ID. The problem with doing it like this is that userInfomationName is not getting added.
      • 2
    • So you want to have somethig like this: rootRef -> userInformations -> pushId -> userInformationId and userInformationName right?

According to the last comemnts, to achieve a schema that looks like this:

rootRef
  |
  --- userInformations
         |
         --- pushId
              |
              --- "userInformationId: "-LXO5 ... H-o3-P" 
              |
              --- "userInformationName: "spamemail0100@gmail.com"

Please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userInformationsRef = rootRef.child("userInformations");
String userInformationId = userInformationsRef.push().getKey();
String userInformationName = "spamemail0100@gmail.com";
UserInformation userInformation = new UserInformation(userInformationId, userInformationName);
userInformationsRef.child(userInformationId).setValue(userInformation);
  • 1
Reply Report