我有这个简单的云功能:
export const getTasks = functions.https.onRequest((request, response) => {
admin.firestore().collection('tasks').get()
.then(snapshot => {
const results = []
snapshot.forEach(task => {
const data = task.data()
results.push(data)
})
response.send(results)
})
.catch(error => {
console.log(error)
response.status(500).send(error)
})
});
来自浏览器的 https 调用给了我一个正确的 json:
[
{
title: "A title",
dueDate: "2018-07-03T18:33:27.537Z",
isComplete: true,
type: "task",
date: "2018-07-02T18:33:27.537Z"
},
{
type: "task",
date: "2018-07-02T18:36:25.506Z",
title: "Wowo",
dueDate: "2018-07-02T21:59:59.000Z",
isComplete: true
},
{
title: "Abc",
dueDate: "2018-07-04T18:31:58.050Z",
isComplete: false,
type: "task",
date: "2018-07-02T18:31:58.050Z"
}
]
但是当我尝试通过该函数从 iOS 客户端接收数据时,我得到一个 FIRHTTPSCallableResult 对象和一个 nil 对象:
functions.httpsCallable("getTasks").call() { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
//...
}
// ...
}
print( "result -> \(type(of: result))")
print( "result?.data -> \(type(of: result?.data))")
日志:
result -> Optional<FIRHTTPSCallableResult>
result?.data -> Optional<Any>
我尝试使用 JSON 解析,但它不起作用。如何获取 json?
谢谢
最佳答案
API documentation for the data
field状态:
The data is in the form of native objects. For example, if your trigger returned an array, this object would be an NSArray. If your trigger returned a JavaScript object with keys and values, this object would be an NSDictionary.
由于您要从函数发送对象数组,因此您会将 data
的内容视为 NSDictionary
对象的 NSArray
.
关于ios - 使用 Swift 解析来自 Firebase Cloud Function 的 FIRHTTPSCallableResult 对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181504/