使用Swift从Firebase Cloud Function解析FIRHTTPSCallableResult对象数据

时间:2018-07-04 22:44:39

标签: ios json swift firebase google-cloud-functions

我有这个简单的Cloud Function:

DateTimeOffset

来自浏览器的https调用为我提供了正确的json:

/actions
/components
  /ButtonComponent
/reducers

但是当我尝试通过该功能从iOS客户端接收数据时,我得到了一个 FIRHTTPSCallableResult 对象和一个nil对象:

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)
    })
});

日志:

[
{
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"
}
]

我尝试使用JSON解析,但是它不起作用。如何获取json?

谢谢

1 个答案:

答案 0 :(得分:1)

API documentation for the data field指出:

  

数据采用本机对象的形式。例如,如果您的   触发器返回一个数组,该对象将是一个NSArray。如果你的   触发器返回了一个带有键和值的JavaScript对象,该对象   会是NSDictionary。

由于要从函数中发送对象数组,因此将data的内容视为NSArray对象的NSDictionary