Swift/Firebase:无法在 Firebase 云函数上发送请求数据
Swift/Firebase: Can't send request data on a Firebase Cloud Function
我似乎无法通过请求正文从 Swift iOS 应用向 Firebase 发送标识符。
我的 javascript 代码有效(我在 API 构建器中测试过它):
exports.numberOfVideos = functions.https.onRequest((request, response) => {
var attributionID = request.body.data.attributionID || "No attributionID found.";
functions.logger.info('[LOG]: request.body has a value of ' + JSON.stringify(request.body), {structuredData: true});
var db = admin.firestore();
db.collection("content").get().then(snapshot => {
var total = 0;
snapshot.forEach(doc => {
if (doc.data().attributionID == attributionID) {
total++;
}
// stuff = stuff.concat(newelement);
});
response.send('{"data": {"attributionID": "' + attributionID + '", "numberOfContents": "' + total + '"}}');
return "";
}).catch(reason => {
response.send(reason)
})
});
我的Swift代码看不到传送数据:
static func getContentCount(for obj: Object, completion: @escaping (_ count: Int?) -> Void) {
let httpsCallable = functions.httpsCallable("numberOfVideos")
// httpsCallable.setValue("application/json", forKey: "Content-Type")
httpsCallable.call(["attributionID" : obj.id]) { 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(error)
}
}
if let response = result?.data as? [String: [String: Any]] {
if let data = response["data"] {
if let countAsAString = data["numberOfContents"] as? String {
let count = Int(countAsAString)
if count != nil {
completion(count)
} else {
print("Data is valid but no count found: \(result?.data)")
completion(nil)
}
}
}
} else {
print("Data is invalid: \(result?.data)")
completion(nil)
}
}
}
当它在 REST API 测试仪上运行时,正文如下:
POST /numberOfVideos HTTP/1.1
Host: *****************
Content-Type: application/json
Content-Length: 49
{"data":{"attributionID":"biNiaWVtmjUKoTQ1fTZu"}}
如有任何帮助,我们将不胜感激!
已修复。我必须使用 NSDictionary,而不是字典:
NSDictionary(dictionary: ["attributionID" : obj.id])
我似乎无法通过请求正文从 Swift iOS 应用向 Firebase 发送标识符。
我的 javascript 代码有效(我在 API 构建器中测试过它):
exports.numberOfVideos = functions.https.onRequest((request, response) => {
var attributionID = request.body.data.attributionID || "No attributionID found.";
functions.logger.info('[LOG]: request.body has a value of ' + JSON.stringify(request.body), {structuredData: true});
var db = admin.firestore();
db.collection("content").get().then(snapshot => {
var total = 0;
snapshot.forEach(doc => {
if (doc.data().attributionID == attributionID) {
total++;
}
// stuff = stuff.concat(newelement);
});
response.send('{"data": {"attributionID": "' + attributionID + '", "numberOfContents": "' + total + '"}}');
return "";
}).catch(reason => {
response.send(reason)
})
});
我的Swift代码看不到传送数据:
static func getContentCount(for obj: Object, completion: @escaping (_ count: Int?) -> Void) {
let httpsCallable = functions.httpsCallable("numberOfVideos")
// httpsCallable.setValue("application/json", forKey: "Content-Type")
httpsCallable.call(["attributionID" : obj.id]) { 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(error)
}
}
if let response = result?.data as? [String: [String: Any]] {
if let data = response["data"] {
if let countAsAString = data["numberOfContents"] as? String {
let count = Int(countAsAString)
if count != nil {
completion(count)
} else {
print("Data is valid but no count found: \(result?.data)")
completion(nil)
}
}
}
} else {
print("Data is invalid: \(result?.data)")
completion(nil)
}
}
}
当它在 REST API 测试仪上运行时,正文如下:
POST /numberOfVideos HTTP/1.1
Host: *****************
Content-Type: application/json
Content-Length: 49
{"data":{"attributionID":"biNiaWVtmjUKoTQ1fTZu"}}
如有任何帮助,我们将不胜感激!
已修复。我必须使用 NSDictionary,而不是字典:
NSDictionary(dictionary: ["attributionID" : obj.id])