无法部署Firebase云功能

时间:2019-01-09 02:08:22

标签: typescript firebase cors google-cloud-functions ionic4

我正在尝试使用cors部署Firebase云功能,但是它不起作用。这是下面的代码,有人可以帮我吗?

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import * as cors from 'cors'

admin.initializeApp()

const corsHandler = cors({origin: true})

export const getUser = functions.https.onRequest((request, response) => {

    corsHandler(request, response, () => {})

    admin.firestore().doc("profiles/T0XCPHPkrJM4I10okb9KSHyukqn1").get()
    .then(snapshot => {
        const data = snapshot.data()
        console.log(data)
        response.send(data)
    })
    .catch(error => {
        console.log(error)
        response.status(500).send(error)
    })

})

1 个答案:

答案 0 :(得分:1)

以下应该起作用。您必须在功能内 内包含异步Firebase调用的代码。

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import * as cors from 'cors'

admin.initializeApp()

const corsHandler = cors({origin: true})

export const getUser = functions.https.onRequest((request, response) => {

    corsHandler(request, response, () => {

        admin.firestore().doc("profiles/T0XCPHPkrJM4I10okb9KSHyukqn1").get()
        .then(snapshot => {
            const data = snapshot.data()
            console.log(data)
            response.send(data)
        })
        .catch(error => {
            console.log(error)
          response.status(500).send(error)
        })

    })

})