简体   繁体   English

如何检查特定的 FirebaseFirestore 错误代码?

[英]How to perform a check for a particular FirebaseFirestore error code?

I have the following code:我有以下代码:

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
                    }
                }
        );

I would like to update my Firestore database if the collection already exists.如果集合已经存在,我想更新我的 Firestore 数据库。 Otherwise, I would like Firestore to create a new collection.否则,我希望 Firestore 创建一个新集合。

I would like to create a new collection only if the collection is not found.仅当找不到该集合时,我才想创建一个新集合。 Essentially, I would like to do the check like this:本质上,我想做这样的检查:

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
                            }
                        }
                    }
            );

How can I go about achieving this?我怎样才能实现这一目标?

That should be possible if you cast the exception to a feedbackFirebaseFirestoreException object and then check its getCode() value against the possible Firestore error codes .如果您将异常转换为feedbackFirebaseFirestoreException对象,然后根据可能的Firestore 错误代码检查其getCode()值,这应该是可能的。

So something like:所以像:

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
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM