Snefru Clone
Snefru Clone

Reputation: 85

Firebase Functions, Response in not a valid json object

i am developing a role-based application using android studio, since my project needs admin rights for creating user and deleting user i used firebase admin sdk for my project. I tried to delete multiple accounts but i faced with issue. The response returns not a valid json object. As in my code i tried to handle possible errors. However the response still returns not a valid json object. See below

index.js

    exports.deleteUser = functions.https.onCall(async (data,context) => {
try {
    if(!context.auth) {
        throw new AuthenticationError('Kimlik Doğrulaması Yapılmamış');
    }
    const uids = JSON.parse(data);
    console.log(uids);
    const callerUid = context.auth.uid;
    const callerUser = await admin.auth().getUser(callerUid);
    if(!callerUser.customClaims.admin && !callerUser.customClaims.superadmin) {
        throw new NotAnAdminError('Bu işlemi sadece yöneticiler gerçekleştirebilir');
    } 
    const reference = admin.firestore().collection("Users");
    const res = await admin.auth().deleteUsers(uids);
    res.errors.forEach(element => console.log(element));
    const successes = res.successCount;
    const fails = res.failureCount;
    console.log(fails);
    console.log(successes);
    if(fails===0) {
        await uids.forEach(element => reference.doc(element).delete());
        return {result:successes+' Öğrenci Silindi!'};
    }else {
        throw new functions.https.HttpsError('Silme Hatası','Bilinmeyen hata, 
        silinemeyen öğrenci sayısı: '+fails);
    }
}
    catch(error) {
        if(error.type === 'NotAnAdminError') {
            throw new functions.https.HttpsError('Bu işlemi yapma yetkiniz 
             yok.',error.message);
        }else if(error.type === 'AuthenticationError') {
            throw new functions.https.HttpsError('Kimlik Hatası',error.message);
        }else {
            throw new functions.https.HttpsError('internal ERROR from catch 
            block',error.message);
        }
     }
});

Android Code

    private Task<String> deleteUsers(List<CheckableUser> users) {
    List<String> idlist = new ArrayList<>();
    for (CheckableUser user:users) {
        idlist.add(user.getUid());
    }
    return mFunctions.getHttpsCallable("deleteUsers").call(jsonFormatted).
    continueWith(new Continuation<HttpsCallableResult, String>() {
        @Override
        public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {               
        HashMap<String,Object> data = task.getResult().getData();
        String result = data.get("result");
        Log.d(TAG, "then: "+result);
        return result;
        }
    });
mFunctions.getHttpsCallable("deleteUsers").call(jsonFormatted).continueWith(new 
Continuation<HttpsCallableResult, String>() {
        @Override
        public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
            HashMap<String,Object> data = task.getResult().getData();
            String result = data.get("result");
            Log.d(TAG, "then: "+result);
            return result;
        }
    });
}


deleteUsers(users).addOnCompleteListener(new OnCompleteListener<String>() {
                        @Override
                        public void onComplete(@NonNull Task<String> task) {
                            if (task.isSuccessful()) {
                                Snackbar snackbar = 
Snackbar.make(requireView().findViewById(R.id.constraintlayout),"Selected users are 
deleted",4000);
                                snackbar.show();
                            }else {
                                debugFirebase(task.getException());
                            }
                        }
                    });


private void debugFirebase(Exception e) {
    if (e instanceof FirebaseFunctionsException) {
        FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
        Log.d(TAG, "debugFirebase: MESSAGE: "+ffe.getMessage());
        Log.d(TAG, "debugFirebase: CODE: "+ffe.getCode());
        Log.d(TAG, "debugFirebase: DETAILS: "+ffe.getDetails());
        Log.e(TAG, "debugFirebase: EXCEPTION: ",ffe );
    }
}

Exception

com.google.firebase.functions.FirebaseFunctionsException: Response is not valid 
JSONobject.
Caused by: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be 
converted to JSONObject

Upvotes: 0

Views: 495

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317808

Your function is declared and exported as "deleteUser", but you are calling it in the Android client as "deleteUsers", which is not the same. The strings need to match.

Upvotes: 2

Related Questions