简体   繁体   English

如何从Firebase中的函数取回数据

[英]How to retrieve data back from a function in firebase

I can see that the function is now working in the cloud functions log, It sends back the users information based on their email, however, I can't seem to retrieve that data back in the app client, it just comes through as Optional(<FIRHTTPSCallableResult: 0x2825b0b70>) 我可以看到该功能现在正在云功能日志中运行,它根据用户的电子邮件向用户发送信息,但是,我似乎无法在应用客户端中检索到该数据,它只是作为Optional(<FIRHTTPSCallableResult: 0x2825b0b70>)

Here's my function code: 这是我的功能代码:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
admin.initializeApp()

exports.uniqueEmail = functions.https.onCall((data) => {
  const email = data.email;
  if (!email) {
    throw new functions.https.HttpsError('invalid-argument', 'Missing email parameter');
  }
  admin.auth().getUserByEmail(email)
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log('Successfully fetched user data:', userRecord.toJSON());
    return "true"
  })
  .catch(function(error) {
   console.log('Error fetching user data:', error);
   return "false"
  });
});

Here's my swift code, trying to print to the console the data that I should be retrieving back: 这是我的快速代码,试图将要检索的数据打印到控制台:

else if (email != "" && password == ""){
    print("testing...")
    let functions = Functions.functions()
    functions.httpsCallable("uniqueEmail").call(["email": email]) { (result, error) in
        if let error = error as NSError? {
            if error.domain == FunctionsErrorDomain {
                let code = FunctionsErrorCode(rawValue: error.code)
                let message = error.localizedDescription
                let details = error.userInfo[FunctionsErrorDetailsKey]
                print("CODE:", code, " ","Message:", message, " ","DETAILS:", details)
            }
            // ...
        }
        print(result)
        if let text = (result?.data as? [String: Any])?["email"] as? String {
            let output = text
            print(output)
        }
    }
}

Right now the top-level of your Cloud Functions code returns nothing though, so you'll want to fix that by adding a return on the top-level: 现在,尽管您的Cloud Functions代码的顶层未返回任何内容,所以您需要通过在顶层添加return来解决此问题:

exports.uniqueEmail = functions.https.onCall((data) => {
  const email = data.email;
  if (!email) {
    throw new functions.https.HttpsError('invalid-argument', 'Missing email parameter');
  }
  return admin.auth().getUserByEmail(email).then(function(userRecord) {
    console.log('Successfully fetched user data:', userRecord.toJSON());
    return {"taken" : "true"}
  }).catch(function(error) {
   console.log('Error fetching user data:', error);
   return({"taken": "false", error});
  });
});

With the above, the code that calls the Cloud Function gets back a JSON object, which in Swift translates to a dictionary: [String, Any] . 通过上面的代码,调用Cloud Function的代码将返回一个JSON对象,该对象在Swift中转换为字典: [String, Any]

I eventually got to the right answer and my new code for my swift file (it was an error in there) is: 我最终找到了正确的答案,而我的快速文件的新代码(那里是错误)是:


let functions = Functions.functions()
            functions.httpsCallable("uniqueEmail").call(["email": email]) { (result, error) in
                if let error = error as NSError? {
                    if error.domain == FunctionsErrorDomain {
                        let code = FunctionsErrorCode(rawValue: error.code)
                        let message = error.localizedDescription
                        let details = error.userInfo[FunctionsErrorDetailsKey]
                        print("CODE:", code, " ","Message:", message, " ","DETAILS:", details)
                    }
                }
                let output = result?.data as! String
                print(output)
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM