user6086991
user6086991

Reputation:

android - Adding a Custom ID to Firestore document

i"m working on a project that contains a code like this one: How to add Document with Custom ID to firestore (Angular)

When I'm about to try the app, it always crashes. But the code is all the same.

The error that I get in the LogCat is this:

Invalid document reference. Document references must have an even number of segments, but Users has 1

My full code is this:

public class RegisterActivity extends AppCompatActivity {


private EditText registerUsername;
private EditText registerEmail;
private EditText registerPassword;
private EditText registerConfirmPassword;
private Button registerRegisterButton;
private Button registerLoginButton;
private ProgressBar registerProgressBar;

private FirebaseFirestore firebaseFirestore;

private String user_id;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    registerUsername = findViewById(R.id.register_username);
    registerEmail = findViewById(R.id.register_email);
    registerPassword = findViewById(R.id.register_password);
    registerConfirmPassword = findViewById(R.id.register_confirm_password);
    registerRegisterButton = findViewById(R.id.register_register_button);
    registerLoginButton = findViewById(R.id.register_login_button);
    registerProgressBar = findViewById(R.id.register_progressBar);

    firebaseFirestore = FirebaseFirestore.getInstance();

    user_id = registerUsername.getText().toString();

    registerLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(loginIntent);

        }
    });

    registerRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String username = registerUsername.getText().toString();
            String email = registerEmail.getText().toString();
            String password = registerPassword.getText().toString();
            String confirmPassword = registerConfirmPassword.getText().toString();

            if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirmPassword)) {

                if (password.equals(confirmPassword)) {

                    registerProgressBar.setVisibility(View.VISIBLE);

                    Map<String, String> usersMap = new HashMap<>();
                    usersMap.put("username", username);
                    usersMap.put("email", email);
                    usersMap.put("password", password);

                    firebaseFirestore.collection("Users").document(user_id).set(usersMap).addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {

                            Toasty.success(RegisterActivity.this, "Successfully Registered", Toast.LENGTH_SHORT).show();

                            Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(loginIntent);

                            registerProgressBar.setVisibility(View.INVISIBLE);

                        }
                    });

                } else {

                    Toasty.error(RegisterActivity.this, "Passwords Don't Match", Toast.LENGTH_SHORT).show();

                }

            }

        }
    });
}}

I want the "user_id" to be the document id and NOT another id generated by Firestore.

Can someone please help? And Thanks in advance.

Upvotes: 1

Views: 2389

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317968

You're setting user_id at the beginning of your onCreate, probably when the EditText registerUsername doesn't contain any text. This means user_id will always be empty, no matter what happens in this activity.

You can't pass an empty string to document(). You need an actual string value there that's also a valid Firestore document ID. Try getting the string value in registerUsername after they've filled out the form, not before.

Upvotes: 1

Related Questions