简体   繁体   English

我无法将数据写入 Firebase Firestore

[英]I am unable to write data to Firebase Firestore

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.我无法在 Firestore 上编写它,我尝试了许多不同的可能性。

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.就像为什么我可能会发生这种情况一样,或者我是否需要在 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这是 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.因为我已经在 SharedPreferances 中保存了用户 UID 值,所以我在那里调用了它。 Did I do some wrong in the above code?我在上面的代码中做错了吗?

在此处输入图片说明

在此处输入图片说明

I do have the value of User UID.我确实有用户 UID 的价值。

You can't send data to Firestore, because you send to firestore false data.您无法向 Firestore 发送数据,因为您向 Firestore 发送了虚假数据。 You send nothing, only UserUID field with no information:您什么都不发送,只发送没有信息的UserUID字段:

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

UserUID - is field which you send to dB. UserUID - 是您发送到 dB 的字段。 Then you wrote “” - it's false, because you're sending empty info, you have to give variable.然后你写了“” - 这是错误的,因为你发送的是空信息,你必须给出变量。

After continuous coding, I have found that it's not possible to call the Method inside "task.isSuccessfully" during the signup.经过不断的编码,我发现在注册过程中无法调用“task.isSuccessfully”中的方法。 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.我所做的是为了在“mAuth.createUserWithEmailAndPassword()”方法之外获取 UserID,我初始化了 FirebaseUser 并调用了当前用户,并且我能够获得 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.但是 Firebase 需要大约 2 到 3 秒来加载 UserID,所以我添加了 ProgressBar,它最终构成了加载时间,并且我能够在 DocumentReference 中成功调用 UserID 引用。

WHICH SOLVED MY ISSUE.这解决了我的问题。

However, I am only able to get the USERID while debugging, if I run the I get null reference.但是,我只能在调试时获取 USERID,如果我运行 I get null 引用。

If you want to set UserUID as the document in Firestore , write:如果要将 UserUID 设置为 Firestore 中的文档,请编写:

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

-This currentUser variable - is the id of authorized user, his id in your app. - 这个 currentUser 变量 - 是授权用户的 id,他在你的应用程序中的 id。

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

-here you send user id as Firestore document. - 在这里您将用户 ID 作为 Firestore 文档发送。

*it works only when you have created mAuth.createUserWithEmailAndPassword Method, because so your user will have his unique id. *它仅在您创建了 mAuth.createUserWithEmailAndPassword 方法时才有效,因为这样您的用户将拥有他的唯一 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM