从Android调用的Firebase云函数可以工作,但总是得到空响应

从Android调用的Firebase云函数可以工作,但总是得到空响应,android,node.js,firebase,google-cloud-functions,Android,Node.js,Firebase,Google Cloud Functions,我试图调用一个Google Firebase云函数,该函数只需使用一个事务来检查Firestore中是否存在某些内容,然后删除它和另一个文档。我在安卓上点击一个按钮就调用了这个函数,它确实起作用了,条目被删除了,但是我在安卓上得到的响应是空的,而不是我试图发回的消息 云功能: //Takes in the uid of the user who sent the request and the user who received it, // and deletes the request ex

我试图调用一个Google Firebase云函数,该函数只需使用一个事务来检查Firestore中是否存在某些内容,然后删除它和另一个文档。我在安卓上点击一个按钮就调用了这个函数,它确实起作用了,条目被删除了,但是我在安卓上得到的响应是空的,而不是我试图发回的消息

云功能:

//Takes in the uid of the user who sent the request and the user who received it,
// and deletes the request
exports.cancelFriendRequest = functions.https.onCall((data, context) => {
    //Grab the parameters
    const sender = data.sender;
    const recipient = data.recipient;

    //Ensure parameters are good
    if(sender === undefined || sender === "" || recipient === undefined || recipient === "") {
        return res.status(400).send("Invalid arguments.");
    }

    const db = admin.firestore();

    //Build document references to check that friend request exists
    let receivedFriendRequestDocRef = db.doc("users/"+recipient+"/receivedFriendRequests/"+sender);
    let sentFriendRequestDocRef = db.doc("users/"+sender+"/sentFriendRequests/"+recipient);

    db.runTransaction(transaction => {
        return transaction.getAll(receivedFriendRequestDocRef, sentFriendRequestDocRef).then(docs => {
            //Check that the friend request exists
            if(!docs[0].exists) {
                return Promise.reject(new Error("Friend request does not exist."));
            }

            //Delete friend requests
            transaction.delete(receivedFriendRequestDocRef);
            transaction.delete(sentFriendRequestDocRef);

            return Promise.resolve("Friend request deleted successfully.");
        });
    }).then(result => {
        //I've also tried return res.status(200).send("Success: " + result);
        //But that wasn't working so I thought I'd try this, which I saw in a Google sample git repo
        return "Success: " + result;
    }).catch(err => {
        return err.toString();
    });
});
Android云函数调用:

public static Task<String> cancelFriendRequest(String sender, String recipient) {
        FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();

        //Create the arguments to the callable function
        Map<String, Object> data = new HashMap<>();
        data.put("sender", sender);
        data.put("recipient", recipient);

        return mFunctions
                .getHttpsCallable("cancelFriendRequest")
                .call(data)
                .continueWith(new Continuation<HttpsCallableResult, String>() {
                    @Override
                    public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                        //This continuation runs on either success or failure, but if the task
                        // has failed then getResult() will throw an Exception which will be
                        // propagated down.
                        String result = (String) task.getResult().getData();
                        return result;
                    }
                });
    }
公共静态任务取消请求(字符串发送方、字符串接收方){
FirebaseFunctions mFunctions=FirebaseFunctions.getInstance();
//创建可调用函数的参数
映射数据=新的HashMap();
数据输入(“发送方”,发送方);
数据输入(“收件人”,收件人);
返回函数
.getHttpScalable(“取消请求”)
.呼叫(数据)
.continueWith(新的continueWith(){
@凌驾
公共字符串(@NonNull Task)引发异常{
//此延续在成功或失败时运行,但如果任务
//失败,则getResult()将引发异常,该异常将
//向下传播。
字符串结果=(字符串)task.getResult().getData();
返回结果;
}
});
}
Android中调用云函数的函数:

private static void cancelFriendRequest(String uid) {
    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    CloudFunctions.cancelFriendRequest(userId, uid).addOnCompleteListener(new OnCompleteListener<String>() {
        @Override
        public void onComplete(@NonNull Task<String> task) {
            String result = task.getResult();
            Log.e("Result", "" + result);
            Snackbar.make(mRecyclerView, result, Snackbar.LENGTH_LONG).show();
        }
    });
}
private静态无效取消请求(字符串uid){
字符串userId=FirebaseAuth.getInstance().getCurrentUser().getUid();
cancelFriendRequest(userId,uid).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
字符串结果=task.getResult();
Log.e(“结果”,“结果+结果”);
Snackbar.make(mRecyclerView,result,Snackbar.LENGTH_LONG.show();
}
});
}
我很乐意提供更多的信息。我还发现调用这个函数非常慢。如果我将其设置为onRequest并通过手动导航到URL来调用它,它的工作速度会非常快,但是onCall和从Android调用速度非常慢。无论如何,这是另一个问题,谢谢


编辑:此外,Snackbar会弹出(为空,因为它返回null),在文档实际从数据库中删除之前10-15秒。

您错过了主函数中的返回(在云函数代码中),因此该函数会在事务完成执行之前返回。从:

使用这些推荐的方法来管理您的产品的生命周期 职能:

解析执行异步处理的函数(也称为 作为“后台函数”),返回JavaScript承诺

尝试返回事务(
return db.runTransaction(transaction=>{….});
),它应该可以工作,如下所示:

//Takes in the uid of the user who sent the request and the user who received it,
// and deletes the request
exports.cancelFriendRequest = functions.https.onCall((data, context) => {
    //Grab the parameters
    const sender = data.sender;
    const recipient = data.recipient;

    //Ensure parameters are good
    if(sender === undefined || sender === "" || recipient === undefined || recipient === "") {
        return res.status(400).send("Invalid arguments.");
    }

    const db = admin.firestore();

    //Build document references to check that friend request exists
    let receivedFriendRequestDocRef = db.doc("users/"+recipient+"/receivedFriendRequests/"+sender);
    let sentFriendRequestDocRef = db.doc("users/"+sender+"/sentFriendRequests/"+recipient);

   return db.runTransaction(transaction => {
        return transaction.getAll(receivedFriendRequestDocRef, sentFriendRequestDocRef).then(docs => {
            //Check that the friend request exists
            if(!docs[0].exists) {
                return Promise.reject(new Error("Friend request does not exist."));
            }

            //Delete friend requests
            transaction.delete(receivedFriendRequestDocRef);
            transaction.delete(sentFriendRequestDocRef);

            return Promise.resolve("Friend request deleted successfully.");
        });
    }).then(result => {
        //I've also tried return res.status(200).send("Success: " + result);
        //But that wasn't working so I thought I'd try this, which I saw in a Google sample git repo
        return "Success: " + result;
    }).catch(err => {
        return err.toString();
    });
});

非常感谢!我一定是直接忘记把它放在那里了,因为当我看到这是你的回答时,我可以发誓我已经归还了它,但唉,我没有。再次感谢!