【问题标题】:firebase serve: From a locally served app, call locally served functionsfirebase serve:从本地服务的应用程序调用本地服务的函数
【发布时间】: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


【解决方案1】:

更新 - 2021 年 4 月

截至 2021 年 4 月,方法 useFunctionsEmulator 已被弃用。建议改用useEmulator(host, port)方法。


原帖:

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

@gregbkr 在github 线程中找到了解决方法。

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

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

确保在部署到 SERVER 时将其移除

【讨论】:

    【解决方案2】:

    目前不支持对此类可调用函数进行本地测试。该团队正在努力为您指定可调用函数的 URL 端点,以便您可以将其重定向到不同的位置进行测试。

    【讨论】:

    • 无赖!这不仅仅是测试。这对本地开发很有帮助。
    • 为什么不让 firebase serve 配置 CORS 以在开发时允许 localhost?它失败了...请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'localhost:4200' 因此不允许访问。
    • @Doug Stevenson,难道没有办法为firebase.initializeApp 中的所有函数指定端点吗?谢谢!
    • 有没有人尝试使用hosts 文件将请求重定向到本地主机?
    【解决方案3】:

    刚刚找到解决方法。 使用 fiddlers AutoResponder 将函数调用重定向到本地服务函数。

    第一步

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

    第二步

    复制本地服务函数的url

    第 3 步

    激活自动应答器并使用以下规则 (第二条规则对于允许所有外部请求也很重要

    【讨论】:

      【解决方案4】:

      这对我有用,谢谢@GorvGoyl!

      script src="/__/firebase/init.js?useEmulator=true"></script
      

      【讨论】:

        猜你喜欢
        • 2018-10-15
        • 2011-07-01
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 2017-10-08
        相关资源
        最近更新 更多