【问题标题】:What are the requirements to deploy firebase functions?部署 Firebase 功能有哪些要求?
【发布时间】:2022-10-25 13:34:43
【问题描述】:

我正在尝试通过 index.ts 文件将一个非常简单的函数部署到 firebase 函数。

使用的部署命令是: firebase deploy --only functions

这是 index.ts 文件:

import { getDatabase, ref, get, child } from "firebase/database";

import * as functions from "firebase-functions";
import * as cors from "cors";
import * as express from "express";
import * as bodyParser from "body-parser";

import { AnyExercise, Workout } from "./types";

const app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// Automatically allow cross-origin requests
app.use(cors({ origin: true }));

// parse application/json
app.use(bodyParser.json());

const dbRef = ref(getDatabase());

app.get("/workouts/:userId", (req, res) => {
  get(child(dbRef, `workouts/${req.params.userId}`))
    .then((snapshot) => {
      if (snapshot.exists()) {
        console.log("workouts", snapshot.val());
        return snapshot.val();
      } else {
        console.log("No data available");
        return null;
      }
    })
    .then((workouts: Record<Workout["id"], Workout>) => {
      if (!workouts) {
        res.send({ workoutObj: {}, exerciseListByWorkout: {} });
      }
      get(child(dbRef, "exercises")).then((snapshot) => {
        if (snapshot.exists()) {
          const exercises: Record<AnyExercise["id"], AnyExercise> =
            snapshot.val();
          console.log("exercises", exercises);
          const mappedExercises: Record<Workout["id"], AnyExercise[]> = {};
          Object.values(workouts).forEach((workout) => {
            const exList: any[] = [];
            workout.exerciseList?.forEach((exId) => {
              const ex = exercises && exercises[exId];
              if (ex) {
                exList.push(ex);
              }
            });
            mappedExercises[workout.id] = exList;
          });
          res.send({ workoutObj: [], exerciseListByWorkout: mappedExercises });
        } else {
          console.log("No data available");
          res.send({ workoutObj: workouts, exerciseListByWorkout: {} });
        }
      });
    })
    .catch((error) => {
      console.error(error);
      res.send(error);
    });
});

// Expose Express API as a single Cloud Function:
export const api = functions.https.onRequest(app);

这是我尝试部署它时遇到的错误:

Error: Failed to load function definition from source: Failed to generate manifest from 
function source: FirebaseError: Firebase: Need to provide options, when not being deployed 
to hosting via source. (app/no-options).

我只是不明白错误在说什么。在这种情况下,我需要提供哪些消息选项?

【问题讨论】:

  • 你有没有先添加SDK并初始化实时数据库?请在realtime database 上查看此文档。
  • 我之前的评论是否解决了您的担忧?
  • 是的,谢谢!

标签: node.js typescript firebase google-cloud-functions


【解决方案1】:

您需要先添加实时数据库 JS SDK 并初始化实时数据库。基于此documentation

您必须在初始化 JavaScript SDK 时指定您的实时数据库 URL。

您可以在 Firebase 控制台的实时数据库部分找到您的实时数据库 URL。根据数据库的位置,数据库 URL 将采用以下形式之一:

  • https://[DATABASE_NAME].firebaseio.com(用于 us-central1 中的数据库)
  • https://[DATABASE_NAME].[REGION.firebasedatabase.app(适用于所有其他位置的数据库)

使用以下代码 sn -p 初始化 SDK:

import { initializeApp } from "firebase/app";
import { getDatabase } from "firebase/database";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
  // The value of `databaseURL` depends on the location of the database
  databaseURL: "https://[DATABASE_NAME].firebaseio.com",
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);


// Initialize Realtime Database and get a reference to the service
const database = getDatabase(app);

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 2021-02-26
    • 2018-07-01
    • 2019-02-05
    • 2017-11-15
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    相关资源
    最近更新 更多