[英]How do I call the function with a parameter I set up in firebase functions
I've deployed this code to my firebase functions project: 我已将此代码部署到我的firebase函数项目中:
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
admin.initializeApp()
export const getEmail = functions.https.onRequest((request, response) => {
var from = request.body.sender;
admin.auth().getUserByEmail(from)
.then(snapshot => {
const data = snapshot.toJSON()
response.send(data)
})
.catch(error => {
//Handle error
console.log(error)
response.status(500).send(error)
})
})
Which takes in a email parameter that it gets from the user's input on my app. 它接收来自用户在我的应用程序上的输入的电子邮件参数。 My app's code looks like this:
我的应用程序代码如下所示:
Functions.functions().httpsCallable("https://us-central1-projectname.cloudfunctions.net/getEmail").call(email) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
//email isnt taken
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
print(code, message, details)
}
// ...
}
if let text = (result?.data as? [String: Any])?["text"] as? String {
// email taken
}
}
When I run the app and when that function is called, it seems to do nothing, no error message is shown and no data has been sent back. 当我运行应用程序并且调用该函数时,它似乎什么都不做,没有显示错误消息,也没有发回任何数据。 What am I missing?
我错过了什么?
Update: I went to the logs and nothing has happened in there as if the function was never called. 更新:我去了日志,没有发生任何事情,好像从未调用过该函数。
You are actually mixing up HTTP Cloud Functions and Callable Cloud Functions : 您实际上正在混合HTTP云功能和可调用云功能 :
You Cloud Function code corresponds to an HTTP one but the code in your front-end seems to call a Callable one. 云功能代码对应于HTTP功能,但前端的代码似乎称为可调用代码。
You should adapt one or the other, most probably adapt your Cloud Function to a Callable one, along the following lines: 您应该根据以下几点调整其中一个,最有可能使您的云功能适应可调用的功能:
exports.getEmail = functions.https.onCall((data, context) => {
const from = data.sender;
return admin.auth().getUserByEmail(from)
.then(userRecord => {
const userData = userRecord.toJSON();
return { userData: userData };
})
});
Have a look at the doc for more details, in particular how to handle errors. 有关更多详细信息,请查看文档,特别是如何处理错误。 The doc is quite detailed and very clear.
该文件非常详细,非常清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.