【发布时间】:2018-07-02 15:54:21
【问题描述】:
如何在本地正确模拟云功能,以便它拥有在 firebase 服务器上调用时的所有数据? (例如
context.auth)
我正在使用firebase serve 为我的项目提供服务,它在http://localhost:5000/ 上运行正常,但是,我的云函数是从https://us-central1-<my-app>.cloudfunctions.net/getUser 调用的。 (功能甚至没有部署。)
为了避免 XY 问题,我正在尝试调试我的函数,但从 firebase shell 调用它会导致 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))
【问题讨论】:
-
我在这里面临同样的问题。你是怎么解决的?谢谢。
-
@cbdev420 AFAIR 我没有。我只是使用了详尽的
console语句(如上)和firebase控制台中的日志...... -
这对我有用:对于本地,您必须调用(在 firebase.initializeApp 之后):
firebase.functions().useFunctionsEmulator('http://localhost:5000')source. It's on the 2nd answer to the question on this link
标签: firebase google-cloud-functions