【问题标题】:Access Firebase Realtime database/Firestore from Cloud Function via Firebase Emulator通过 Firebase Emulator 从 Cloud Function 访问 Firebase 实时数据库/Firestore
【发布时间】:2022-01-04 21:55:14
【问题描述】:

我想使用 postman 之类的工具或我的 react 本机应用程序来调用 Firebase Cloud 函数并从云函数中访问身份验证、Firestore、存储或实时数据库模拟器。只有 Admin SDK 似乎可以工作,但它无法连接到模拟器中的数据。

模拟器

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://localhost:4000                │
└─────────────────────────────────────────────────────────────┘

┌────────────────┬────────────────┬─────────────────────────────────┐
│ Emulator       │ Host:Port      │ View in Emulator UI             │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Authentication │ localhost:9099 │ http://localhost:4000/auth      │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Functions      │ localhost:5001 │ http://localhost:4000/functions │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Firestore      │ localhost:8080 │ http://localhost:4000/firestore │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Database       │ localhost:9000 │ http://localhost:4000/database  │
└────────────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at localhost:4400
  Other reserved ports: 4500

我有什么

index.js

const functions = require("firebase-functions");
const { queries } = require("../../utils");

exports.countries = functions.https.onCall(async (data, context) => {
  try {
    const allCountries = await queries.getAllCountries();

    return allCountries;
  } catch (error) {
    console.log("error getting countries  :", error);
    return null;
  }
});

queries.js

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

exports.getAllCountries = async function getAllCountries() {
  console.log("getting all countries ");

  let result = [];

  await admin
    .database()
    .ref("Countries")
    .once("value")
    .then((data) => {
      let countries = data.val();

      countries = countries.map(({ countryId, name, emoji }) => {
        return {
          id: countryId,
          name,
          emoji,
        };
      });

      result.push(...countries);
    })
    .catch((error) => {
      console.log("error finding country name", error);
      return null;
    });

  return result;
};

我需要什么

(在我的 getAllCountries 函数中,我想调用 Realtime db 模拟器而不是连接到 PROD 数据库的管理员)

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

    //call real time databse emulator
   // i would like this call to reach localhost:9000/realtimedatabase 
       const allCountries = await functions.database
          .ref("countries")
          .then((result) => {})
          .catch((err) => {});
    
        return allCountries;

我收到一条错误消息,提示 functions.database.ref 不是函数。

【问题讨论】:

    标签: firebase react-native firebase-realtime-database google-cloud-functions firebase-tools


    【解决方案1】:

    如果您要从 Firebase 实时数据库模拟器中检索一些数据,请首先确保您导入了一些数据。您可以访问此link,了解如何将数据导出和导入到 Firebase RTDB 模拟器。

    启动模拟器并将数据导入 RTDB 的示例命令:

    firebase emulators:start --import=./dir
    

    为了确认,您将收到如下日志:

    i  database: Importing data from /usr/local/username/home/username/application/dir/sample-rtdb.json
    

    关于这个错误:

    functions.database.ref 不是函数。

    您不能仅使用firebase-functions 从函数访问您的数据库; firebase-functions 不足以访问数据库。您需要使用诸如 Firebase Admin SDK 之类的 SDK 并将其初始化以访问 reference。根据documentation,如果您使用Admin SDK并且您的代码在Cloud Functions模拟器中运行,您的项目ID和其他配置将在调用initalizeApp时自动设置。

    // As an admin, the app has access to read and write all data, regardless of Security Rules
    const functions = require("firebase-functions");
    const admin = require("firebase-admin");
    admin.initializeApp();
    
    var db = admin.database();
    var ref = db.ref("restricted_access/secret_document");
    ref.once("value", function(snapshot) {
      console.log(snapshot.val());
    });
    

    【讨论】:

    • 如何指定导入到 Firestore 或实时数据库?我使用了这个命令 firebase emulators:start --only auth,functions,firestore,database --import=./data 它总是进入实时数据库
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2019-08-07
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2020-10-19
    • 2020-10-19
    相关资源
    最近更新 更多