Javascript Firebase函数,响应不是有效的json对象
我正在使用android studio开发一个基于角色的应用程序,因为我的项目需要管理员权限来创建用户和删除用户。我在项目中使用了firebase admin sdk。我试图删除多个帐户,但我面临的问题。响应返回的不是有效的json对象。在我的代码中,我试图处理可能出现的错误。但是,响应仍然返回无效的json对象。 见下文 index.js 安卓代码Javascript Firebase函数,响应不是有效的json对象,javascript,android,node.js,firebase,google-cloud-functions,Javascript,Android,Node.js,Firebase,Google Cloud Functions,我正在使用android studio开发一个基于角色的应用程序,因为我的项目需要管理员权限来创建用户和删除用户。我在项目中使用了firebase admin sdk。我试图删除多个帐户,但我面临的问题。响应返回的不是有效的json对象。在我的代码中,我试图处理可能出现的错误。但是,响应仍然返回无效的json对象。 见下文 index.js 安卓代码 私有任务删除用户(列表用户){ List idlist=new ArrayList(); for(CheckableUser用户:用户){ add
私有任务删除用户(列表用户){
List idlist=new ArrayList();
for(CheckableUser用户:用户){
add(user.getUid());
}
返回mFunctions.getHttpScalable(“deleteUsers”).call(jsonFormatted)。
continueWith(新的continueWith(){
@凌驾
公共字符串(@NonNull Task Task)引发异常{
HashMap data=task.getResult().getData();
字符串结果=data.get(“结果”);
Log.d(标记“then:”+结果);
返回结果;
}
});
mFunctions.getHttpScalable(“deleteUsers”).call(jsonFormatted).continueWith(新
续(){
@凌驾
公共字符串(@NonNull Task)引发异常{
HashMap data=task.getResult().getData();
字符串结果=data.get(“结果”);
Log.d(标记“then:”+结果);
返回结果;
}
});
}
deleteUsers(users).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
弹簧撑杆弹簧撑杆=
Snackbar.make(requireView().findViewById(R.id.constraintlayout),“选定的用户是
删除“,4000);
snackbar.show();
}否则{
debugFirebase(task.getException());
}
}
});
私有void debugFirebase(例外e){
if(FirebaseFunctionsException的实例){
FirebaseFunctionsException ffe=(FirebaseFunctionsException)e;
Log.d(标记“debugFirebase:MESSAGE:”+ffe.getMessage());
Log.d(标记“debugFirebase:CODE:”+ffe.getCode());
Log.d(标记“debugFirebase:DETAILS:+ffe.getDetails());
Log.e(标记“debugFirebase:EXCEPTION:”,ffe);
}
}
例外情况
com.google.firebase.functions.FirebaseFunctionsException:响应无效
JSONobject。
原因:org.json.JSONException:Value您的函数被声明并导出为“deleteUser”,但您在Android客户端将其称为“deleteUsers”,这是不同的。字符串需要匹配
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