从Android调用的Firebase云功能可以正常工作但始终获得空响应

时间:2018-05-18 18:32:15

标签: android node.js firebase google-cloud-functions

我正在尝试调用Google Firebase云功能,该功能仅使用事务来检查我的Firestore中是否存在某些内容,然后将其删除,另外还有一个Document。我通过点击按钮调用Android中的函数,它确实有效,条目被删除,但我在Android中回复的响应是空的,而不是我试图发回的消息。

云功能:

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

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

我很乐意提供更多信息。我也发现调用函数真的很慢。如果我将它设置为onRequest并通过手动导航到URL来调用它,它的工作速度非常快,但onCall和从Android调用的速度非常慢。无论如何,这是另一个问题,谢谢!

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

1 个答案:

答案 0 :(得分:1)

您错过了主函数中的返回(在云函数代码上),因此函数在事务执行完毕之前返回。来自documentation

  

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

     

解析执行异步处理的函数(也称为   as&#34;后台函数&#34;)返回一个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();
    });
});