如何执行一个特定的FirebaseFirestore错误代码检查?



我有以下代码:

db.collection("property").document(userUID).update(propertyRoot).addOnSuccessListener(
new OnSuccessListener<Void>(){
@Override
public void onSuccess(Void aVoid)
{
//Success
}
}
).addOnFailureListener(
new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception e){
//Create a new collection and set
}
}
);

如果集合已经存在,我想更新我的Firestore数据库。否则,我希望Firestore创建一个新的集合。

我想创建一个新的集合,只有当集合没有找到。基本上,我想这样做检查:

db.collection("property").document(userUID).update(propertyRoot).addOnSuccessListener(
new OnSuccessListener<Void>(){
@Override
public void onSuccess(Void aVoid)
{
//Success
}
}
).addOnFailureListener(
new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception e){
if(e.errorCode == FirebaseFirestoreException.NOT_FOUND)
{
//Create a new collection and set
}
}
}
);

我怎样才能做到这一点?

如果您将异常强制转换为feedbackFirebaseFirestoreException对象,然后根据可能的Firestore错误代码检查其getCode()值,那么这应该是可能的。

比如:

public void onFailure(@NonNull Exception e){
if (e is FirebaseFirestoreException) {
if ((e as FirebaseFirestoreException).getCode() == FirebaseFirestoreException.Code.NOT_FOUND)
{
//Create a new collection and set
}
}
}

相关内容

最新更新