我正在尝试使用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)
})
})
答案 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)
})
})
})