Firebase 函数,响应不是有效的 json 对象

Posted

技术标签:

【中文标题】Firebase 函数,响应不是有效的 json 对象【英文标题】:Firebase Functions, Response in not a valid json object 【发布时间】:2021-01-21 17:27:41 【问题描述】:

我正在使用 android studio 开发一个基于角色的应用程序,因为我的项目需要管理员权限来创建用户和删除用户,我为我的项目使用了 firebase admin sdk。我试图删除多个帐户,但我遇到了问题。响应返回无效的 json 对象。就像在我的代码中一样,我试图处理可能的错误。但是,响应仍然返回不是有效的 json 对象。 见下文

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

安卓代码

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

异常

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

【问题讨论】:

【参考方案1】:

您的函数被声明并导出为“deleteUser”,但您在 Android 客户端中将其调用为“deleteUsers”,这不一样。字符串需要匹配。

【讨论】:

以上是关于Firebase 函数,响应不是有效的 json 对象的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 可调用函数在 Android SDK 中不返回 JSON 响应

Flutter Firebase cloud_functions 包抛出错误:内部

检查来自 API 的响应是不是为有效 JSON

在本地调用 Firebase 云函数时,我收到 200 响应,但响应是 cors 类型响应,而不是我的实际数据

如何判断一个字符串是不是是有效的 JSON?

Google Cloud Functions - 客户端调用错误(Android Studio):FirebaseFunctionsException:响应不是有效的 JSON 对象