Reputation: 4244
I have a Firebase cloud function checkUsers
. Which works in postman like the screenshot attached.
This is the cloud function
app.post('/', (req, res) => {
if (req.body.emails === undefined) {
res.status(422).send("Missing email addresses input");
return;
}
const emailsToCheck = req.body.emails;
if (emailsToCheck.length == 0) {
res.status(422).send("No email addresses provided");
return;
}
var existingEmails = [];
var emailsChecked = [];
listAllUsers(function(page) {
page.forEach(function(record) {
existingEmails.push(record.toJSON()["email"].toLowerCase());
});
}, function(lastPage) {
// Here, we should have all the users
lastPage.forEach(function(record) {
existingEmails.push(record.toJSON()["email"].toLowerCase());
});
// Iterating over emailsToCheck and checking if the email is in existingEmails
emailsToCheck.forEach(function(record) {
const exists = existingEmails.includes(record);
emailsChecked.push({ email: record, exists: exists });
});
res.status(200).send(emailsChecked);
});
});
exports.checkUsers = functions.https.onRequest(app);
When I try to call the same function from iOS SDK, it does not work.
func checkEmails(
_ contacts: [SIContact],
_ completion: @escaping (([SIContact]) -> Void)
) {
let data = ["aaa", "[email protected]", "bbb"]
let params = ["emails": data]
self.functions.httpsCallable("checkUsers").call(params) { (result, error) in
if let error = error {
print("Error while checking emails \(error)")
return completion(contacts)
}
guard let results = result?.data as? [[String: Any]] else {
return completion(contacts)
}
print(results)
}
}
UPDATE:
I am getting 422, Missing email addresses input error. That should be only returned if the emails parameter is not found in the request, as we can see in the cloud function code above.
I have tried to debug it, and my conclusion is the iOS code is not passing the emails array properly.
Upvotes: 0
Views: 932
Reputation: 317467
You're mixing up callable functions with HTTP functions. They work differently. You can't use the Firebase SDK for callable function to invoke some HTTP endpoint. If you want to invoke an HTTP endpoint, you will need to use an HTTP library for your platform.
Upvotes: 2