How to pass parameters to the cloud function of firebase

▼魔方 西西 提交于 2021-01-28 02:52:58

问题


I am using Android.

After the user logs in to firebase, how can I retrieve the user information in the firestore according to the account?

My function is in the cloud function of firebase. My code as follows:

mFunctions.getHttpsCallable("getInfo").call()
            .continueWith(new Continuation<HttpsCallableResult, Object>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {

                    Log.d("-----TEST-----", "BEGIN000");
                    Object result =  task.getResult().getData();
                    Log.d("-----TEST-----", "SUCCESS");
                    self_tel.setText((Integer) result);
                    return (String) result;
                }
            }).addOnCompleteListener(new OnCompleteListener<Object>() {
        @Override
        public void onComplete(@NonNull Task<Object> task) {
            if (!task.isSuccessful()) {
                Exception e = task.getException();
                if (e instanceof FirebaseFunctionsException) {
                    FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                    FirebaseFunctionsException.Code code = ffe.getCode();
                    Object details = ffe.getDetails();
                }

                // [START_EXCLUDE]
                Log.w("test", "addMessage:onFailure", e);
                //showSnackbar("An error occurred.");
                return;
                // [END_EXCLUDE]
            }
            String result = (String) task.getResult();
            Log.d("-----TEST-----", "SUCCESS1");

        }
    });

回答1:


If you want assistance using firebase-firestore you can use the inbuild assistant. Goto Tools -> Firebase and follow the instructions.

You can pass parameters to the firebase function using HashMap

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

and pass it using .call(data)

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

return mFunctions
        .getHttpsCallable("getInfo")
        .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;
            }
        });

For more instructions Call functions from your app



来源:https://stackoverflow.com/questions/51888994/how-to-pass-parameters-to-the-cloud-function-of-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!