Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
firebase服务:从本地服务的应用程序调用本地服务的函数_Firebase_Google Cloud Functions - Fatal编程技术网

firebase服务:从本地服务的应用程序调用本地服务的函数

firebase服务:从本地服务的应用程序调用本地服务的函数,firebase,google-cloud-functions,Firebase,Google Cloud Functions,如何在本地正确模拟云函数,使其拥有firebase服务器上调用时的所有数据?(例如上下文.auth) 我正在使用firebase-service为我的项目提供服务,它在http://localhost:5000/,但是,正在从https://us-central1-.cloudfunctions.net/getUser。(该功能甚至没有部署。) 为了避免XY问题,我正在尝试调试我的函数,但从firebase shell调用它会导致上下文。auth未定义,从通过postman调用时也是如此http

如何在本地正确模拟云函数,使其拥有firebase服务器上调用时的所有数据?(例如
上下文.auth

我正在使用
firebase-service
为我的项目提供服务,它在
http://localhost:5000/
,但是,正在从
https://us-central1-.cloudfunctions.net/getUser
。(该功能甚至没有部署。)

为了避免XY问题,我正在尝试调试我的函数,但从firebase shell调用它会导致
上下文。auth
未定义,从
通过postman调用时也是如此http://localhost:5000//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))

目前不支持这样的可调用函数的本地测试。团队正在研究一种方法,让您指定可调用函数的URL端点,以便您可以将其重定向到其他位置进行测试。

刚刚找到了一种解决方法。 使用Fiddler自动应答器将函数调用重定向到本地服务函数

步骤1

从客户端复制函数的目标url

步骤2

复制本地服务函数url

步骤3

激活自动重新排序器并使用以下规则 (第二条规则也很重要,允许所有的请求


更新日期:2021年4月

自2021年4月起,方法
useFunctionsEmulator
已被弃用。建议改用方法
usemulator(主机、端口)


原创帖子:

默认情况下,
firebase-service
将查询发送到云函数而不是localhost,但可以将其更改为指向localhost

@gregbkr在这个线程中找到了一个解决方法

基本上是在html头部中的firebase初始化脚本(firebase/init.js)之后添加这个

<script>
   firebase.functions().useFunctionsEmulator("http://localhost:5001");
</script>

firebase.functions().useFunctionsEmulator(“http://localhost:5001");

在部署到服务器时,请确保将其删除。

Bummer!这不仅仅是测试。这在本地开发时会很有帮助。为什么不让firebase serve在开发时将CORS配置为允许本地主机?它会失败,因为请求的资源上没有“Access Control allow Origin”标头。Origin“”是refore not allowed access.@Doug Stevenson,有没有办法为所有函数指定端点,如
firebase.initializeApp
?谢谢!有人尝试过使用
hosts
文件将请求重定向到本地主机吗?我在这里遇到了同样的问题。你是如何解决的?谢谢。@cbdev420 AFAIR我没有。我只是用了排气管ive
console
语句(如上所述)和firebase控制台中的日志……这对我来说很有用:对于本地,您必须调用(在firebase.initializeApp之后):
firebase.functions().UseFunctionsAssemblator('http://localhost:5000)