我正在尝试从 Firestore 获取数据,但如果文档具有特殊字符,即 São Paulo,则得到 null
,并且它只发生在 São Paulo。
工作查询:
db.collection("streets")
.document("Arlington")
.collection("street")
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
List<DocumentSnapshot> docSnap = task.getResult().getDocuments();
}
});
无效查询:
db.collection("streets")
.document("São Paulo")
.collection("street")
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
List<DocumentSnapshot> docSnap = task.getResult().getDocuments();
}
});
如果文档有特殊字符,如何从文档中获取数据?
最佳答案
Firestore 中的特殊字符没有问题。名称为 São Paulo
的文档应该可以正常工作。正如我在您的架构中看到的那样,该文档中没有任何字段,这就是为什么也用 italic
编写的原因。因此,如果您尝试获取该文档,这是预期的行为。
如果您想要获取集合中的所有文档,您需要像下面的代码行一样遍历 task.getResult()
:
yourCollectionRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
编辑:
db.collection("streets").document("São Paulo").collection("street").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getString("city"));
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
关于java - 如何从 Firestore 中具有特殊字符的文档中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53560176/