简体   繁体   English

无法从我的前端调用 Firebase 云功能

[英]Can't call firebase cloud function from my frontend

I've been trying for at least an hour now.我已经尝试了至少一个小时。 The function makeUserAdmin is deployed to firebase.函数makeUserAdmin被部署到makeUserAdmin I also have other cloud functions that DO run (triggered by auth.user().onCreate() and auth.user().onDelete() .我还有其他可以运行的云函数(由auth.user().onCreate()auth.user().onDelete()
Look at the line with the Chinese comment, isn't that how I'm supposed to reference a cloud function from the frontend?看看有中文注释的那一行,这不是我应该如何从前端引用云函数的方式吗? Apparently, I don't know exactly how to import that functionality, because typescript is not giving me any help/autocomplete.显然,我不知道如何导入该功能,因为打字稿没有给我任何帮助/自动完成。
Must I import something related to firebase-functions in my index.html via CDN?我必须通过 CDN 在我的 index.html 中导入与 firebase-functions 相关的内容吗? (btw I have other services imported via CDN like that. (顺便说一句,我有通过 CDN 导入的其他服务。

// @ts-ignore
import * as firebase from 'firebase';

// @ts-ignore
// import * as functions from 'firebase-functions';

// const firebase = require("firebase");
// // Required for side-effects
// require("firebase/functions");

// @ts-ignore
const makeAdminForm: HTMLFormElement = document.querySelector('#makeAdmin')!;

makeAdminForm.addEventListener('submit', (e: Event) => {
    e.preventDefault();
    makeAdminForm.reset();                                                     //this code executes

    const email: string = makeAdminForm.makeAdminEmail.value;
    //TODO check if acc with that email address exists

    var languagesString: string = makeAdminForm.makeAdminLanguages.value;
    languagesString = languagesString.replace(/\s+/g, ' '); //replace multiple consecutive spaces by one space
    //TODO check with regex, only lowercase letters and spaces
    const languagesList: string[] = languagesString.trim().split(" ");

    const makeUserAdmin = firebase().functions.httpsCallable('makeUserAdmin'); //你看这里
    makeUserAdmin({ //context must not be passed manually
        emailToMakeAdmin: email
    }).then((result: object) => {
        console.log(result);
    });
});

I know the function makeUserAdmin doesn't get called because no new logs are created in the firebase console (however, new logs are created with the other functions)我知道函数makeUserAdmin没有被调用,因为在makeUserAdmin中没有创建新日志(但是,新日志是用其他函数创建的)

You need to add the Firebase products that you want to use (ie the Cloud Functions for Firebase Client SDK in your case), as follows:您需要添加要使用的 Firebase 产品(即您的案例中的 Cloud Functions for Firebase Client SDK),如下所示:

// Firebase App (the core Firebase SDK) is always required and must be listed first
import * as firebase from "firebase/app";

// Add the Firebase products that you want to use
import "firebase/functions";

see the doc here ("Using module bundlers" tab) and here .请参阅 此处的文档(“使用模块捆绑器”选项卡)和 此处

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM