【问题标题】:How to pass parameters to the cloud function of firebase如何将参数传递给firebase的云函数
【发布时间】:2019-01-24 02:51:17
【问题描述】:

我正在使用 Android。

用户登录firebase后,如何根据账号找回firestore中的用户信息?

我的功能在firebase的云功能中。 我的代码如下:

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

        }
    });

【问题讨论】:

  • 发布您的代码。
  • mFunctions.getHttpsCallable("getInfo").call() .continueWith(new Continuation() { @Override public String then(@NonNull Task task) 抛出异常 { Log.d("-----TEST-----", "BEGIN000"); 对象结果 = task.getResult().getData(); Log.d("-----TEST--- --", "SUCCESS"); self_tel.setText((Integer) result); return (String) result; } })
  • 不在这里,用问题更新它,以便人们可以帮助你。
  • 好的,这是我第一次使用stackoverflow。哈哈

标签: android firebase google-cloud-functions


【解决方案1】:

如果您在使用 firebase-firestore 时需要帮助,可以使用内置助手。 转到 Tools -&gt; Firebase 并按照说明进行操作。

您可以使用 HashMap 将参数传递给 firebase 函数

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

并使用.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;
            }
        });

更多说明Call functions from your app

【讨论】:

  • 如果要给云函数的“getInfo”函数传参数怎么办?
  • 意思是call()是用来传递参数的?
  • 好的,但是为什么我不能在云函数中调用getInfo()?他说CPU超出配额。 call()中的参数是Json格式的吗?
  • 格式为hashmap。并从服务器作为 json 访问。按照链接上的说明实现你的云函数和调用函数,看看是否还会出现错误。
  • 错误:超出配额(函数调用中的 CPU 分配:每天,函数调用中的 CPU 分配:每天);要增加配额,请通过console.cloud.google.com/billing?project=meeting-zyw 在您的项目中启用计费。无法执行功能。日志显示:
猜你喜欢
  • 2020-10-01
  • 1970-01-01
  • 2020-03-14
  • 2021-04-28
  • 2018-06-14
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
相关资源
最近更新 更多