Reputation:
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>)
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)
}
}
}
Upvotes: 0
Views: 275
Reputation:
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)
}
}
Upvotes: 0
Reputation: 599131
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:
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]
.
Upvotes: 1