【问题标题】:How to get data from Request sent through HTTP callable function?如何从通过 HTTP 可调用函数发送的请求中获取数据?
【发布时间】:2020-07-14 11:27:30
【问题描述】:

我正在使用 Firebase Admin SDK 来完成管理功能。我已部署的功能位于控制台中的 Firebase Cloud Function 上。我已经能够从应用程序中调用这些函数,但是我不知道从我通过调用发送的映射数据中获取值。如果我是正确的,那应该可以通过request 变量进行检索,但是当我尝试诸如request.email 之类的东西时,这似乎不是一个有效的论点。

MainActivity.java

public Task<String> callCloudFunction() {
            FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();
            Map<String, Object> data = new HashMap<>();
            data.put("email", "new@example.com");
            data.put("password", "changePassword");
            data.put("displayName", "Mike Example");
            data.put("trainerId", "Mike Example");
            data.put("photoURL", "NULL");

            return mFunctions
                    .getHttpsCallable("mkUser")
                    .call(data)
                    .continueWith(new Continuation<HttpsCallableResult, String>() {
                        @Override
                        public String then(@NonNull Task<HttpsCallableResult> task) {
                            // 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.
                            return (String) task.getResult().getData();
                        }
                    });

    }

index.ts

export const mkUser = functions.https.onRequest((request, response) =>{

  admin.auth().createUser({
      email: request.email, //IS NOT A VALID ARGUMENT ('email' CANNOT BE FOUND)
      emailVerified: false,
      password: 'secretPassword',
      displayName: 'John Doe', //VALUES ARE HARDCODED BUT I WOULD LIKE TO USE 'Mike Example' SENT FROM MAIN ACTIVITY
      photoURL: 'http://www.example.com/12345678/photo.png',
      disabled: false
    })
      .then(function(userRecord: { uid: any; }) {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log('Successfully created new user:', userRecord.uid);
      })
      .catch(function(error: any) {
        console.log('Error creating new user:', error);
      });
});

【问题讨论】:

    标签: node.js typescript httprequest android-json


    【解决方案1】:

    所以我已经弄清楚了我遇到的问题以及如何解决这个问题。

    首先,在 index.ts 中,我使用的 HTTP 函数是 .onRequest,但如果我将其更改为 .onCall,那么我用来在 MainActivity.java 中调用 HTTP 函数的代码将可以工作.

    使用.onCall 将接收到的信息从“请求”更改为“数据”;然后可以使用它来访问通过应用程序内的调用发送的映射信息。

    index.ts

    export const mkUser = functions.https.onCall(async (data, context) =>{
      try {
        const userRecord = await admin.auth().createUser({
          email: data.email,
          emailVerified: false,
          password: data.password,
          displayName: data.displayName,
          disabled: false
        })
        console.log('Successfully created new user:', userRecord.uid);
       } catch (error) {
        console.log('Error creating new user:', error);
        return "ERROR"
       }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2017-10-24
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      相关资源
      最近更新 更多