【发布时间】: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