【发布时间】:2018-12-13 08:34:38
【问题描述】:
我有这个简单的云函数:
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?
谢谢
【问题讨论】:
标签: ios json swift firebase google-cloud-functions