部署 firebase 函数 - 没有创建 Firebase 应用“[DEFAULT]”

Deploy firebase function - No Firebase App '[DEFAULT]' has been created

我正在尝试部署 firebase-cloud 函数:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const app = require("express")();
const { getAuth, createUserWithEmailAndPassword } = require("firebase/auth");
const auth = getAuth();
const firebase = require("firebase/app");

admin.initializeApp();

const config = {
  apiKey: "",
  authDomain: " ",
  projectId: " ",
  storageBucket: " ",
  messagingSenderId: " ",
  appId: " ",
  measurementId: " ",
};

firebase.initializeApp(config);
//Signup Route
app.post("/signup", (req, res) => {
  const newUser = {
    email: req.body.email,
    password: req.body.password,
  };
  createUserWithEmailAndPassword(auth, newUser.email, newUser.password)
    .then((data) => {
      return res
        .status(201)
        .json({ message: `user ${data.user.uuid} signed up successfully` });
    })
    .catch((err) => {
      console.error(err);
      return res.status(500).json({ error: err.code });
    });
});

exports.api = functions.https.onRequest(app);

但是我得到了这个错误

FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).

您似乎试图在此处导入客户端 JavaScript/Web SDK:

const { getAuth, createUserWithEmailAndPassword } = require("firebase/auth");
const auth = getAuth();
const firebase = require("firebase/app");

Cloud Functions 不支持该 SDK。相反,您应该使用 Node.ks 的 Admin SDK,您已经通过以下方式导入它:

const admin = require("firebase-admin");

使用 Admin SDK create a user

admin
  .auth()
  .createUser({
    email: 'user@example.com',
    emailVerified: false,
    phoneNumber: '+11234567890',
    password: 'secretPassword',
    displayName: 'John Doe',
    photoURL: 'http://www.example.com/12345678/photo.png',
    disabled: false,
  })