gpt4 book ai didi

firebase serve : From a locally served app, 调用本地提供的函数

转载 作者:行者123 更新时间:2023-12-04 08:02:23 25 4
gpt4 key购买 nike

How can I properly simulate a cloud function locally so that it has all data as when being invoked on firebase servers? (e.g. the context.auth)



我正在为我的项目提供服务 firebase serve ,它在 http://localhost:5000/ 上运行正常,但是,我的云函数是从 https://us-central1-<my-app>.cloudfunctions.net/getUser 调用的. (该功能甚至没有部署。)

为了避免 XY 问题,我试图调试我的函数,但从 调用它火力炮弹结果 context.auth未定义,通过 调用时相同 postman 来自 http://localhost:5000/<my-app>/us-central1/getUser .

这是我的 ./functions/src/index.ts文件
import * as functions from 'firebase-functions'
import admin from 'firebase-admin'
import { inspect } from 'util'

admin.initializeApp()

export const getUser = functions.https.onCall((data, context) => {
console.debug('== getUser called =========================================')
console.log('getUser', inspect(data), inspect(context.auth))
return admin.database().ref('userRights/admin').child(context.auth.uid).once('value', snapshot => {
console.log(snapshot.val())
if (snapshot.val() === true) {
return 'OK'
// return {status: 'OK'}
} else {
return 'NOK'
// return {status: 'error', code: 401, message: 'Unauthorized'}
}
})
})

文件 ./firebase.functions.ts
import { functions } from '~/firebase'
export const getUser = functions.httpsCallable('getUser')

消费者 ./src/pages/AdminPanel/index.tsx
import { getUser } from '~/firebase.functions'
//...
getUser({myDataX: 'asd'}).then(response => console.debug('response', response))

最佳答案

更新 - 2021 年 4 月
截至 2021 年 4 月,方法 useFunctionsEmulator已被弃用。建议使用方法useEmulator(host, port)相反。

原帖:
默认情况下,firebase serve将查询发送到 CLOUD 函数而不是 localhost,但可以将其更改为指向 localhost。
@gregbkr 在此 github 找到了解决方法线程。
您基本上是在 html head 中的 firebase 初始化脚本 (firebase/init.js) 之后添加它。

<script>
firebase.functions().useFunctionsEmulator("http://localhost:5001");
</script>
确保在部署到服务器时删除它

关于firebase serve : From a locally served app, 调用本地提供的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51139843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com