Ad

Can't Deploy Firebase Cloud Function

I'm trying to deploy a firebase cloud function with cors but it doesn't work. This is my code bellow, can someone help me with this ?

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

})
Ad

Answer

The following should work. You have to include the code of your asynchronous Firebase call inside the function.

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

    })

})
Ad
source: stackoverflow.com
Ad