我制作了https云功能,并使用iOS应用程序向其发出请求。调用Cloud函数,因为我可以看到日志,但是我无法从iOS应用程序发送的请求中获取参数。我尝试了几种不同的选择,但他们帮了我,请告诉我如何才能达到效果?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
module.exports = functions.https.onRequest((req, res) => {
console.log("getChatGroupConversationAvatars start", req.params);
const requestDict = req.params;
console.log("requestDict", requestDict);
const queryFormat = req.query.format;
console.log("queryFormat", queryFormat);
const conversationID = requestDict["conversationID"];
const currentUserID = requestDict["currentUserID"];
console.log("conversationID", conversationID, "currentUserID", currentUserID);
return console.log("getChatGroupConversationAvatars");
});
iOS应用代码
class ChatAvatarsManager {
func getChatGroupConversationAvatars(_ conversationID: String) {
DispatchQueue.main.async {
guard let currentUserID = RealmManager().getCurrentUser()?.id else { return }
DispatchQueue.global(qos: .background).async {
let path = "https://us-central1-exampleapp.cloudfunctions.net/getChatGroupConversationAvatars"
guard let url = URL(string: path) else { return }
let parameters = ["conversationID" : conversationID, "currentUserID" : currentUserID]
AlamofireExampleManager.shared.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
switch response.result {
case .success:
debugPrint("getChatGroupConversationAvatars success")
case .failure(let error):
if error._code == NSURLErrorTimedOut {
//timeout here
debugPrint("getChatGroupConversationAvatars timeOut")
}
debugPrint("getChatGroupConversationAvatars \(error)")
}
}
}
}
}
}
struct AlamofireExampleManager {
static let shared: SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 10
let sessionManager = Alamofire.SessionManager(configuration: configuration)
return sessionManager
}()
例如,我在日志中得到了这个结果
conversationID undefined currentUserID undefined
我也试过了
module.exports = functions.https.onRequest((req, res) => {
const conversationID = req.params.conversationID;
const currentUserID = req.params.currentUserID;
console.log("conversationID", conversationID, "currentUserID", currentUserID);
return console.log("getChatGroupConversationAvatars");
});
答案 0 :(得分:21)
我读了这个post并重写了我的代码,所以它对我有用。
{{1}}
答案 1 :(得分:2)
对于Web :( POST请求)
我们有:
export const myFunction = functions.https.onRequest((req, res) => {
const conversationID = req.body.conversationID;
const currentUserID = req.body.currentUserID;
return ;
});
答案 2 :(得分:0)
[[[FIRFunctions functions] HTTPSCallableWithName:@"functionName"] callWithObject:@{@"key": value}
completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) {
iOS将数据放入request.body.data.key中,因此,如果您要查找来自iOS的传入查询中的数据,那么您正在寻找错误的伙伴。当您使用url中的?key = value参数从浏览器调用该函数时,request.query.key包含数据。但是,当从应用程序中调用时,传递的数据位于request.body.data对象中-我检查了iOS SDK中的源代码,是的,它是如何实现的。