简体   繁体   English

使用 Firebase Cloud Functions 时未定义 httpsCallable

[英]httpsCallable is not defined when using Firebase Cloud Functions

For the past week I've been trying to setup Firebase Cloud Functions, but have been unable to import the dependencies needed.在过去的一周里,我一直在尝试设置 Firebase Cloud Functions,但无法导入所需的依赖项。

This is in my script.js file, my main code:这是在我的 script.js 文件中,我的主要代码:

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

const testFunction = httpsCallable(functions, 'testFunction')

That returns this error:这将返回此错误:

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

In my index.html, I have this:在我的 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>

What am I missing?我错过了什么?

Edit: To upgrade to the new version of Firebase, I removed the script references in my html, did "npm i firebase@9.1.3" in my project folder, moved everything to my script.js file, and this is what it looks like now.编辑:为了升级到新版本的 Firebase,我删除了我的 html 中的脚本引用,在我的项目文件夹中做了“npm i firebase@9.1.3”,将所有内容移到我的 script.js 文件中,这就是它的样子像现在。 But I still get the httpsCallable is not defined error, so the version didn't seem to affect it.但我仍然得到 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()

When using the legacy namespaced syntax (<v8 & v9 compatibility-mode), use:使用旧的命名空间语法(<v8 & v9 兼容模式)时,请使用:

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) => { /* ... */ })

For the modern modular syntax (v9+), use:对于现代模块化语法 (v9+),请使用:

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

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

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

These are documented here .这些都记录在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase functions.httpsCallable(...).then 不是函数 - Firebase functions.httpsCallable(...).then is not a function HttpsCallable云函数的单元测试——在线模式 - Unit testing of HttpsCallable cloud functions - online mode `httpsCallable` 与 `httpsCallableFromURL` 可调用 Firebase 函数 - `httpsCallable` vs. `httpsCallableFromURL` callable Firebase Functions 错误:管理员未使用带有 Cloud Functions for Firebase 的 HTTP 请求定义 - Error: admin is not defined using HTTP request with Cloud Functions for Firebase httpsCallable 在调用 Firebase function 时不是 function - httpsCallable is not a function when calling a Firebase function Firebase.httpsCallable 函数总是返回“超出最大调用堆栈大小” - Firebase .httpsCallable functions always returning “Maximum call stack size exceeded” Firebase Cloud Functions error_:未定义变量 - Firebase Cloud Functions error_: variable is not defined 如何为Firebase.functions()。httpsCallable进行GET? - How do I do a GET for Firebase.functions().httpsCallable? 在 Firebase Cloud Functions 中使用 async/await 时返回一个 Promise - Returning a promise when using async/await in Firebase Cloud Functions 在Firebase Cloud Functions中使用Google Vision API时发生TypeError - TypeError when using Google Vision API in Firebase Cloud Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM