如何将自定义ID设置为集合Firestore?

时间:2019-07-01 18:25:05

标签: android

当我在项目中注册客户并将用户添加到firebase的身份验证中时,我将从auth中获取ID,并将其存储到变量中。现在,我想将其余的信息保存在具有相同id的auth的firestore集合中,但无法正常工作。

private void registerUser() {
    final String first_name_guest = FirstNameGuest.getText().toString();
    final String last_name_guest = LastNameGuest.getText().toString();
    final String email_guest = EmailGuest.getText().toString();
    final String phone_guest = PhoneGuest.getText().toString();
    final String password_guest = PasswordGuest.getText().toString();
    String repassword_guest = rePasswordGuest.getText().toString();

    if (first_name_guest.isEmpty()) {
        FirstNameGuest.setError(getString(R.string.input_error_fname));
        FirstNameGuest.requestFocus();
        return;
    }
    if (last_name_guest.isEmpty()) {
        LastNameGuest.setError(getString(R.string.input_error_lname));
        LastNameGuest.requestFocus();
        return;
    }
    if (email_guest.isEmpty()) {
        EmailGuest.setError(getString(R.string.input_error_email));
        EmailGuest.requestFocus();
        return;
    }

    if (!Patterns.EMAIL_ADDRESS.matcher(email_guest).matches()) {
        EmailGuest.setError(getString(R.string.input_error_email_invalid));
        EmailGuest.requestFocus();
        return;
    }
    if (phone_guest.isEmpty()) {
        PhoneGuest.setError(getString(R.string.input_error_phone));
        PhoneGuest.requestFocus();
        return;
    }

    if (phone_guest.length() != 10) {
        PhoneGuest.setError(getString(R.string.input_error_phone_invalid));
        PhoneGuest.requestFocus();
        return;

    }
    if (password_guest.isEmpty()) {
        PasswordGuest.setError(getString(R.string.input_error_password));
        PasswordGuest.requestFocus();
        return;
    }

    if (password_guest.length() < 6) {
        PasswordGuest.setError(getString(R.string.input_error_password_length));
        PasswordGuest.requestFocus();
        return;
    }
    if (repassword_guest.isEmpty()) {
        rePasswordGuest.setError(getString(R.string.input_error_repassword));
        rePasswordGuest.requestFocus();
        return;
    }
    if (!repassword_guest.equals(password_guest)) {
        rePasswordGuest.setError(getString(R.string.input_error_repassword_match));
        rePasswordGuest.requestFocus();
    }

    mAuth.createUserWithEmailAndPassword(email_guest, password_guest)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    final Guest g;
                    if (task.isSuccessful()) {

                        g = new Guest(
                                first_name_guest,
                                last_name_guest,
                                email_guest,
                                phone_guest
                        );

                        FirebaseDatabase.getInstance().getReference("Guest")
                                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                                .setValue(g).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {

                                if (task.isSuccessful()) {
                                    Toast.makeText(RegisterCustomerActivity.this, getString(R.string.registration_success), Toast.LENGTH_LONG).show();

                                } else {
                                    //display a failure message
                                }
                            }
                        });


                        mAuth.signInWithEmailAndPassword(email_guest, password_guest).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                guestid = task.getResult().getUser().getUid();
                                System.out.println(guestid);
                            }
                        });

                        Map<String, Object> guest = new HashMap<>();
                        guest.put("fname", g.fname);
                        guest.put("lname", g.lname);
                        guest.put("mail", g.mail);
                        guest.put("phone", g.phone);

                        db.collection("guests").document().set(guest).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {

                            }
                        });

                        Intent i = new Intent(RegisterCustomerActivity.this, HistoryActivityGuest.class);
                        startActivity(i);

                    } else {
                        Toast.makeText(RegisterCustomerActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
                        EmailGuest.setError(getString(R.string.input_error_email_already_exist));
                        EmailGuest.requestFocus();
                    }
                }
            });


}
    Process: com.awshakam1998.babysitter, PID: 21427
    java.lang.IllegalArgumentException: Invalid document reference. Document references must have an even number of segments, but guests has 1
        at com.google.firebase.firestore.DocumentReference.forPath(com.google.firebase:firebase-firestore@@20.1.0:82)
        at com.google.firebase.firestore.CollectionReference.document(com.google.firebase:firebase-firestore@@20.1.0:111)
        at com.awshakam1998.babysitterapp.RegisterCustomerActivity$2.onComplete(RegisterCustomerActivity.java:207)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:101)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7425)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)```

0 个答案:

没有答案