I am unable to write data to Firebase Firestore

女生的网名这么多〃 提交于 2020-01-29 20:08:20

问题


Any suggestions about why it is happening.

I have used in the below mentioned manner:

FirebaseFirestore dbFirestore;
dbFirestore  = FirebaseFirestore.getInstance();

While Uploading I have used it in this manner:

documentReference= dbFirestore.collection("CLIENT_DETAIL").document(mAuth.getUid());
documentReference.set(userData);

I am not able to write it on Firestore, I tried many different possibilities.

However, sometimes it does write when I try to log in but not when the code execute while signing up. And when it writes and at that point, I am not able to read it even after calling with all the Field Keys.

Any suggestions?? Like why I might happen, or Do I need to have a billing account at GCP. Please, anyone. It's been a day that I am trying to figure it out.

 mDocumentReferance.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            storeType= documentSnapshot.getString("STORE_TYPE");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.i(TAG, "onFailure: unable to fetch the document");
        }
    });

Its is the code for reading it but it doesn't work

Here is the Code on Task.isSuccessfull

 if (task.isSuccessful()) {

                Log.i(TAG, "onComplete: creating folders based type ");
                OnAuth(task.getResult().getUser());
                sendEmailVerification();
                mAuth.signOut();
                SharedPreferences storeNamePreference= getSharedPreferences("UserDetails",MODE_PRIVATE);
                userData.put(RESTAURANT_UID,storeNamePreference.getString("UserUID",""));
                dbFirestore.collection("CLIENT_DETAIL").document(storeNamePreference.getString("UserUID","")).set(userData).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.i(TAG, "onSuccess: Database referance is created");

                        mDialog.dismiss();
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.i(TAG, "onFailure: database referance is not created");
                    }
                });
                Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

Since I have already saved the User UID value at SharedPreferances I an calling it there. Did I do some wrong in the above code?

I do have the value of User UID.


回答1:


You can’t send data to Firestore, because you send to firestore false data. You send nothing, only UserUID field with no information:

userData.put(RESTAURANT_UID,storeNamePreference.getString("UserUID",""));

UserUID - is field which you send to dB. Then you wrote “” - it’s false, because you’re sending empty info, you have to give variable.




回答2:


After continuous coding, I have found that it's not possible to call the Method inside "task.isSuccessfully" during the signup. What I have done is that in order to get the UserID outside the "mAuth.createUserWithEmailAndPassword()" method I have initialized the FirebaseUser and called the current user and I was able to get UserID. But Firebase takes around 2 to 3 seconds to load the UserID so I have added the ProgressBar which ultimately makeup the time to load and I was able to call successfully the UserID reference at DocumentReference.

WHICH SOLVED MY ISSUE.

However, I am only able to get the USERID while debugging, if I run the I get null reference.




回答3:


If you want to set UserUID as the document in Firestore , write:

FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();

-This currentUser variable - is the id of authorized user, his id in your app.

Then write:
dbFirestore.collection("CLIENT_LIST").document(currentUser.getUid()) .set(userData).addOnSuccessListener(new OnSuccessListener) .......

-here you send user id as Firestore document.

*it works only when you have created mAuth.createUserWithEmailAndPassword Method, because so your user will have his unique id.



来源:https://stackoverflow.com/questions/59917986/i-am-unable-to-write-data-to-firebase-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!