Home > other >  How to receive additional information by using firebase in android
How to receive additional information by using firebase in android

Time:06-02

When the user signs up the first time, I use sendEmailVerification by the following code:

 FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
                            String uid = mFirebaseUser.getUid();
                            FirebaseFirestore db = FirebaseFirestore.getInstance();
                            mFirebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        startToast("Plz go to your mail box.");
                                        UserAccount account = new UserAccount(uid, strEmail, strPwd);
                                        db.collection("Basic_users").document(mFirebaseUser.getUid()).set(account).addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void avoid) {
                                                finish();
                                            }
                                        })
                                                .addOnFailureListener(new OnFailureListener() {
                                                    @Override
                                                    public void onFailure(@NonNull Exception e) {
                                                        startToast("failed to register");
                                                    }
                                                });

and here is useAccount.java

public class UserAccount {
    private String idToken; 
    private String strEmail;
    private String strPwd;

    public void setIdToken(String idToken) {
        this.idToken = idToken;
    }

    public void setStrEmail(String strEmail) {
        this.strEmail = strEmail;
    }

    public void setStrPwd(String strPwd) {
        this.strPwd = strPwd;
    }

    public String getIdToken() {
        return idToken;
    }

    public String getStrEmail() {
        return strEmail;
    }

    public String getStrPwd() {
        return strPwd;
    }

    public UserAccount(String idToken, String strEmail, String strPwd) {
        this.idToken = idToken;
        this.strEmail = strEmail;
        this.strPwd = strPwd;
    }

Firebase information after signing up

and as you can see All the information was received accurately.
But I want to receive additional information when user first use the app. SO i tried to receive additional info by following code

  FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user == null) { //IF there's no user information go to registerActiviety
            startActivity(registerActivity.class);
        } else { //else get current user information
            FirebaseFirestore db = FirebaseFirestore.getInstance();
            DocumentReference docRef = db.collection("users").document(user.getUid());
            docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document.exists()) { //if document exist
                            Log.d(TAG, "DocumentSnapshot data: "   document.getData());
                        } else { //else go to MemberinitActivity and receive some 
                                 //additional information
                            Log.d(TAG, "No such document");
                            startActivity(MemberinitActivity.class);
                        }
                    } else {
                        Log.d(TAG, "get failed with ", task.getException());
                    }
                }
            });

and this is memberinfo.java, which has information that i want to receive more.

public class MemberInfo
{
private String idToken; 
private String name;
private String birthday;
private String address;
private String phone_number;

public MemberInfo(String idToken, String name, String birthday,String 
phone_number,String address) {
        this.idToken = idToken;
        this.name = name;
        this.birthday = birthday;
        this.phone_number = phone_number;
        this.address = address;
    }

    public void setIdToken(String idToken) {
        this.idToken = idToken;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getIdToken() {
        return idToken;
    }

    public String getName() {
        return name;
    }

    public String getBirthday() {
        return birthday;
    }

    public String getAddress() {
        return address;
    }

    public String getPhone_number() {
        return phone_number;
    }
}

After checking the log, I found that the program recognized the document as being present but it actually has nothing(null) log of "if document exists"

I think the program was confused since they share the same getcurrentuser() How can I separate two user values and receive additional information?

CodePudding user response:

I think the program was confused since they share the same getcurrentuser()

No, that's not the reason why you aren't getting any data. The problem in your code lies in the fact that you're writing the data to Firestore at the following location:

db.collection("Basic_users").document(mFirebaseUser.getUid())
                //           
  • Related