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

时间:2019-04-28 14:12:57

标签: javascript swift firebase google-cloud-functions

我可以看到该功能现在正在云功能日志中运行,它根据用户的电子邮件回发用户信息,但是,我似乎无法在应用客户端中检索到该数据,它只是通过为Optional(<FIRHTTPSCallableResult: 0x2825b0b70>)

这是我的功能代码:

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

这是我的快速代码,试图将要检索的数据打印到控制台:

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

2 个答案:

答案 0 :(得分:1)

目前,您的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});
  });
});

使用上述方法,调用Cloud Function的代码将返回一个JSON对象,该对象在Swift中转换为字典:[String, Any]

答案 1 :(得分:0)

我最终得到了正确的答案,而我的swift文件的新代码(那里是一个错误)是:


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