- 确保您的应用程序正确集成了Firestore SDK,并正确配置了Firebase控制台中的项目设置。详细信息请参见Firebase文档。
- 确保您的Firestore集合和文档路径正确,并且您有适当的读写权限。您可以使用以下代码检查权限:
Firestore db = FirebaseFirestore.getInstance();
db.collection("your-collection-path").document("your-document-id").get()
.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
// Permission granted, execute CRUD operation here
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Permission denied, handle error here
}
});
- 确保您的CRUD操作的代码正确,并且正确地使用Firestore的API。例如,使用set()方法替换整个文档,使用update()方法更新部分文档。以下是一个示例:
// Replace entire document with new data
Map data = new HashMap<>();
data.put("name", "John Doe");
data.put("age", 30);
Firestore db = FirebaseFirestore.getInstance();
db.collection("your-collection-path").document("your-document-id")
.set(data)
.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Void aVoid) {
// Document successfully written
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Handle error here
}
});
// Update a single field in the document
Firestore db = FirebaseFirestore.getInstance();
db.collection("your-collection-path").document("your-document-id")
.update("name", "Jane Doe")
.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Void aVoid) {
// Field successfully updated
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Handle error here
}
});
- 如果问题