【问题标题】:httpsCallable is not defined when using Firebase Cloud Functions使用 Firebase Cloud Functions 时未定义 httpsCallable
【发布时间】:2021-10-19 20:54:37
【问题描述】:

过去一周我一直在尝试设置 Firebase Cloud Functions,但无法导入所需的依赖项。

这是在我的 script.js 文件中,我的主要代码:

import firebase from "firebase/app"
require("firebase/functions");

const testFunction = httpsCallable(functions, 'testFunction')

返回此错误:

script.js:7 Uncaught ReferenceError: httpsCallable is not defined
    at Object.parcelRequire.script.js.firebase/app (script.js:7)
    at newRequire (script.75da7f30.js:47)
    at script.75da7f30.js:81
    at script.75da7f30.js:120

在我的 index.html 中,我有这个:

    <script src="https://www.gstatic.com/firebasejs/8.6.5/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.6.5/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.6.5/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-functions.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.6.5/firebase-analytics.js"></script>


<script>  
const firebaseConfig = {
    apiKey: "XXXXXXXXXXXX",
    authDomain: "XXXXX-XXXXX.firebaseapp.com",
    databaseURL: "https://XXXXX-XXXXX-default-rtdb.firebaseio.com",
    projectId: "XXXXX-XXXXX",
    storageBucket: "XXXXX-XXXXX.appspot.com",
    messagingSenderId: "XXXXXX",
    appId: "1:XXXXXX:web:XXXXXX"
  };
        // Initialize Firebase
        firebase.initializeApp(firebaseConfig);
        firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
        firebase.analytics()
</script>

我错过了什么?

编辑:为了升级到新版本的 Firebase,我删除了我的 html 中的脚本引用,在我的项目文件夹中做了“npm i firebase@9.1.3”,将所有内容移到我的 script.js 文件中,这是现在的样子。但是我仍然得到 httpsCallable is not defined 错误,所以版本似乎没有影响它。

import firebase from "firebase/compat/app"
import 'firebase/compat/functions'
import 'firebase/compat/auth'
import 'firebase/compat/analytics'
import 'firebase/compat/database'

const firebaseConfig = {
    apiKey: "XXXXXXXXXXXX",
    authDomain: "XXXXX-XXXXX.firebaseapp.com",
    databaseURL: "https://XXXXX-XXXXX-default-rtdb.firebaseio.com",
    projectId: "XXXXX-XXXXX",
    storageBucket: "XXXXX-XXXXX.appspot.com",
    messagingSenderId: "XXXXXX",
    appId: "1:XXXXXX:web:XXXXXX"
  };
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
firebase.analytics()

【问题讨论】:

  • 您已导入 Firebase SDK 的 v8,但正在尝试使用 Firebase SDK 的 v9 的语法。由于这似乎是一个新项目,我建议将所有代码移至 v9,但使用 v9 意味着使用 JavaScript 模块,这可能会引起一些新的麻烦。
  • 您的所有 Firebase 脚本的版本都应该完全匹配。现在,您正在混合至少两个不同的版本。
  • @samthecodingman,谢谢。这实际上不是一个新项目,它几乎完成了,最后一步是添加一个推荐系统,我需要云功能来做。我使用升级到新版本的步骤编辑了我的原始帖子,但这并没有改变未定义 httpsCallable 的原始问题。
  • @DougStevenson 感谢您指出这一点。我升级到 Firebase 的 v9 并完全删除了这些引用,转而使用 npm 导入它,但未定义 httpsCallable 的原始问题并未受到升级的影响。
  • @TheNomadicAspie 你试过samthecodingman's answer吗?

标签: javascript firebase google-cloud-functions


【解决方案1】:

当使用旧的命名空间语法(

import firebase from "firebase/compat/app" // just firebase/app in <v8
import 'firebase/compat/functions'         // just firebase/functions in <v8

const testFunction = firebase.functions().httpsCallable('testFunction')

testFunction({ /* data */ })
  .then((result) => { /* ... */ })
  .catch((err) => { /* ... */ })

对于现代模块化语法 (v9+),使用:

import { getFunctions, httpsCallable } from 'firebase/functions'

const testFunction = httpsCallable(getFunctions(), 'testFunction')

testFunction({ /* data */ })
  .then((result) => { /* ... */ })
  .catch((err) => { /* ... */ })

这些是documented here

【讨论】:

  • 感谢@samthecoding 人。我尝试了该语法并且不再有“未定义”错误,但现在我收到此错误:“CORS 策略已阻止从源 'localhost:1234' 获取 'us-central1-mydomain-9d291.cloudfunctions.net/testFunction' 的访问权限:响应预检请求未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。
  • @TheNomadicAspie 如果该函数存在并且响应,则不会引发该 CORS 错误。当该函数不存在时,Firebase 不会设置 CORS 标头。检查您部署的函数并确保您的testFunction 已正确部署并以该名称存在。
  • 感谢您提供的信息,但已部署。当我运行 firebase deploy --only 函数时,它返回“部署完成”和一个复选标记。反正有没有专门检查有什么问题?我在任何地方都找不到 firebase-debug 日志文件。我确实找到了一个 database-debug.log 和一个 ui-debug.log 但都没有任何错误或有用的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 2020-02-21
  • 2018-01-03
相关资源
最近更新 更多