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

Viewed 5935

While running this code I'm getting error that:

Invalid document reference. Document references must have an even number of segments

But mylist has 1".

Can anyone please help me in this.

public class LoginActivity extends AppCompatActivity {


    TextView textNewUser, textForgotPassword;
    TextInputEditText username, password;
    Button loginBtn;


    String passwordTxtBx, usernameTxtBx;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.login);


        username = findViewById(R.id.userNameInputTxt);
        password = findViewById(R.id.passwordInputTxt);
        loginBtn = findViewById(R.id.loginBtn);

        usernameTxtBx = username.getText().toString();
        passwordTxtBx = password.getText().toString();

        final FirebaseFirestore database = FirebaseFirestore.getInstance();
        final DocumentReference usernameDocument = database.document("mylist/" + usernameTxtBx);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                        usernameDocument.get()
                                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                        if (task.isSuccessful()){
                                            DocumentSnapshot document = task.getResult();
                                            if(document.exists()){
                                                Toast.makeText(LoginActivity.this, "User exist", Toast.LENGTH_SHORT).show();
                                            } else {
                                                Toast.makeText(LoginActivity.this, "No user", Toast.LENGTH_SHORT).show();
                                            }
                                        } else {
                                            Toast.makeText(LoginActivity.this, "Some error", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
            }
        });
2 Answers

You need to identify a document by a document id within a collection. You've given a collection id "mylist" but no document id. usernameTxtBx is probably empty - this is not a valid document id.

I got the solution. Just moved below lines from onCreate() to loginBtn click listener.

            usernameTxtBx = username.getText().toString();
            final FirebaseFirestore database = FirebaseFirestore.getInstance();
            final DocumentReference usernameDocument = database.document("mylist/" + usernameTxtBx);
Related