我有一个包含两个部分的应用程序。第一部分具有条形码扫描仪并将数据添加到数据库中,第二部分读取已添加的数据。第二部分具有两个EditTexts和一个条形码扫描仪。一个EditText用于扫描值,另一个为电子邮件地址。扫描的值将作为文档ID,电子邮件地址作为集合名称。第一部分将数据完美地添加到数据库中。下面是用于读取添加的数据的代码。 .isSuccesssful)”运行正常,实际上返回了文档的路径,这正是它在数据库中的方式。现在的问题是,尽管任务返回了文档的路径,文档仍然不存在。{{3 }}
public void sell_products(){
final String scanned_code = barcodeValue.getText().toString();
final String theShop = shopId.getText().toString();
final String theName = theShop.concat("_Stock");
if(TextUtils.isEmpty(scanned_code)||TextUtils.isEmpty(theShop))
{
Toast.makeText(Sell_Products_Activity.this,"Fields should not
be empty " ,Toast.LENGTH_LONG).show();
return;
}else{
final FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference myItems = db.collection(theName).document(scanned_code);
myItems.get().addOnCompleteListener(new
OnCompleteListener<DocumentSnapshot>(){
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
// task here is successful and the Toast prints the path.
Toast.makeText(Sell_Products_Activity.this, myItems.getPath(), Toast.LENGTH_LONG).show();
DocumentSnapshot document = task.getResult();
Toast.makeText(Sell_Products_Activity.this, document.getId(),
Toast.LENGTH_LONG).show(); // Here the document id is printed perfectly.
if (document.exists()) {
//检查后,无法获取文档,并打印“找不到此类文档”。 //我不知道问题出在哪里,因为任务正确返回了文档的路径 }其他{ Toast.makeText(Sell_Products_Activity.this,“找不到这样的文档”,Toast.LENGTH_LONG).show(); } } //结束sell_products方法
这些是我的数据库规则。允许读写:if request.auth.uid!= null