无效的文档参考。文档引用必须具有偶数个段,但是mylist包含1个

时间:2018-09-18 15:09:09

标签: android firebase google-cloud-firestore

在运行此代码时,出现以下错误:

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

但是我的列表中有1“。

有人可以帮我吗?

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 个答案:

答案 0 :(得分:1)

您需要通过集合中的文档ID来标识文档。您已给集合ID“ mylist”,但没有文档ID。 usernameTxtBx可能为空-这不是有效的文档ID。

答案 1 :(得分:-1)

我找到了解决方案。刚从onCreate()移到loginBtn单击侦听器的下面。

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