简体   繁体   English

无法将数据写入数据库 firebase firestore

[英]Can't write data into database firebase firestore

I manage to insert the data to authentication, but could not insert to database:我设法将数据插入到身份验证中,但无法插入到数据库中:

Code :代码

fullname = findViewById(R.id.Ifullname);
        password = findViewById(R.id.Ipassword);
        email = findViewById(R.id.Iemail);
        RegisterBtn = findViewById(R.id.button);
        fAuth = FirebaseAuth.getInstance();
        fStore = FirebaseFirestore.getInstance();
        loginBtn = findViewById(R.id.LoginBtn);
RegisterBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String FullName = fullname.getText().toString().trim();
                String Password = password.getText().toString().trim();
                String Email = email.getText().toString().trim();
fAuth.createUserWithEmailAndPassword(Email, Password).addOnCompleteListener(new OnCompleteListener<AuthResult>(){
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task){
                        if(task.isSuccessful()){
                            Toast.makeText(Register.this, "User Created.", Toast.LENGTH_SHORT).show();
                            UserID = fAuth.getCurrentUser().getUid();
                            DocumentReference documentReference = fStore.collection("users").document(UserID);
                            Map<String,Object> user = new HashMap<>();
                            user.put("FName", fullname);
                            user.put("Email",email);
                            user.put("Password",password);
//problem seems to be this line  
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Log.d(TAG, "onSuccess: user Profile is created");
                                }

                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.d(TAG, "onFailure:"+e.toString());

                                }
                            });

Error :错误

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.budgetingapp, PID: 11507 java.lang.RuntimeException: Found conflicting getters for name getText on class androidx.appcompat.widget.AppCompatEditText at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.(com.google.firebase:firebase-firestore@@21.4.1:629) at com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-firestore@@21.4.1:377) at com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore@@21.4.1:177) at com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore@@21.4.1:140) at com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore@@21.4.1:104) at com.google.firebase.firestore.util.CustomClassMapper.convertToPlainJavaTypes(com.google.firebase:firebase-firestore@@21.4.1:78) at com.google.firebase.firestore.UserDataReade E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.budgetingapp, PID: 11507 java.lang.RuntimeException: 在 com.google.firebase.firestore.util 的类 androidx.appcompat.widget.AppCompatEditText 上找到了名称 getText 的冲突 getter .CustomClassMapper$BeanMapper.(com.google.firebase:firebase-firestore@@21.4.1:629) 在 com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-firestore@@21.4 .1:377) 在 com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore@@21.4.1:177) 在 com.google.firebase.firestore.util.CustomClassMapper。 serialize(com.google.firebase:firebase-firestore@@21.4.1:140) 在 com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore@@21.4.1:104) ) 在 com.google.firebase.firestore.util.CustomClassMapper.convertToPlainJavaTypes(com.google.firebase:firebase-firestore@@21.4.1:78) 在 com.google.firebase.firestore.UserDataReade r.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@21.4.1:231) at com.google.firebase.firestore.UserDataReader.parseSetData(com.google.firebase:firebase-firestore@@21.4.1:75) at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:166) at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:146) at com.example.budgetingapp.Register$1$1.onComplete(Register.java:95) at com.google.android.gms.tasks.zzj.run(Unknown Source:4) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) r.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@21.4.1:231) 在 com.google.firebase.firestore.UserDataReader.parseSetData(com.google.firebase:firebase-firestore@@21.4.1:75 ) 在 com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:166) 在 com.google.firebase.firestore.DocumentReference.set(com.google.firebase: firebase-firestore@@21.4.1:146)在 com.example.budgetingapp.Register$1$1.onComplete(Register.java:95)在 com.google.android.gms.tasks.zzj.run(未知来源:4)在 android.os.Handler.handleCallback(Handler.java:873) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:193) 在 android.app。 ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android。 internal.os.ZygoteInit.main(ZygoteInit.java:858)

In short, you are trying to add the TextField's themselves to your database, not the strings you created.简而言之,您正在尝试将 TextField 本身添加到您的数据库中,而不是您创建的字符串。

String FullName = fullname.getText().toString().trim();
String Password = password.getText().toString().trim();
String Email = email.getText().toString().trim();
/* ... */
user.put("FName", fullname);
user.put("Email",email);
user.put("Password",password);

I recommend adopting some naming conventions to prevent this.我建议采用一些命名约定来防止这种情况。 I personally recommend <short-object-type><variable-name> written in camelCase .我个人推荐<short-object-type><variable-name>camelCase编写。

Button     #button   -> #btnRegister
Button     #LoginBtn -> #btnLogin
TextField  #fullname -> #txtFullName
TextField  #email    -> #txtEmail
TextField  #password -> #txtPwd

Sometimes it may also be useful to distinguish private object-level variables from local variables, conventionally by adding an m (for my) at the start of the name:有时,区分私有对象级变量和局部变量也很有用,通常通过在名称开头添加m (代表我):

mBtnRegister.setOnClickListener(...);
Button btnScripted = new Button(...);

You should name variables in camelCase (or UPPER_SNAKE_CASE for constants) and not in PascalCase so they aren't misidentified as classes, interfaces, etc. You can actually see this in action by looking at the syntax highlighting of StackOverflow's code blocks.您应该以camelCase命名(或UPPER_SNAKE_CASE为常量)而不是PascalCase命名变量,这样它们就不会被错误地识别为类、接口等。您可以通过查看 StackOverflow 代码块的语法突出显示来实际看到这一点。


String fullname = txtFullName.getText().toString().trim();
String password = txtPwd.getText().toString().trim();
String email = txtEmail.getText().toString().trim();
/* ... */
user.put("fname", fullname);
user.put("email", email);
// do not store passwords!

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

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