在本地测试onCall https云功能

时间:2020-04-04 14:32:09

标签: javascript typescript firebase google-cloud-platform google-cloud-functions

我有这个电话export const notificationsCall = functions.https.onCall((data, context) => { }

当我部署它并从我的iOS应用functions.httpsCallable("getNotifications").call() { (result, error) in }调用它时,一切正常,并且得到了正确的响应。

但是,当我运行firebase emulators:start --only functions并导航到给定的URL http://localhost:5001/post-dach/us-central1/getNotifications

我得到的就是这个结果。 我正在使用Postman,并且正在使用

进行Post请求

'Content-Type': 'application/json'

身体

{
    "data": {
        "aString": "some string"
    }
}

响应

{
    "result": null
}

此外,当我尝试在终端中记录某些内容时,它没有出现,而我得到的只是来自Firebase的大日志。

知道我在做什么错吗?

我的功能

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { Notification, Request } from '../models'
const db = admin.firestore();

export const notificationsCall = functions.https.onCall((data, context) => {
    const notificationsQuery = db.collection('notifications')

    const notifications: Notification[] = [];

    return notificationsQuery.get().then(snapshot => {
        const requestsRef: FirebaseFirestore.DocumentReference[] = [];
        const notIndexes: number[] = [];

        snapshot.forEach((doc) => {
            notifications.push(doc.data() as Notification);
        });

        notifications.forEach((notification, index) => {
            if (notification.requestReference) {
                requestsRef.push(notification.requestReference);
                notIndexes.push(index);
            }
        });

        return db.getAll(...requestsRef);
    }).then((requests) => { // kthen requests
        const packagesRef: FirebaseFirestore.DocumentReference[] = [];

        requests.forEach((req, index) => {
            const requestt = req.data() as Request

            packagesRef.push((requestt).packageReference)
            notifications[index].request = requestt
        })

        return db.getAll(...packagesRef)
    }).then(packages => {
        packages.forEach((pkg, index) => {
            notifications[index].request.package = pkg;
        })

        return { notifications: notifications };
    }).catch(err => { err })

});

0 个答案:

没有答案