【发布时间】:2021-09-25 04:56:43
【问题描述】:
我正在尝试使用 firebase 本地模拟器套件测试我的应用,该套件使用托管、firestore 和云功能。
我使用以下代码为本地模拟器设置我的应用程序,参考https://firebase.google.com/docs/emulator-suite/connect_firestore#web-version-9 中的说明。
import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";
import { initializeApp } from "firebase/app";
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
connectFirestoreEmulator(db, 'localhost', 8080);
然后我可以使用以下命令启动我的本地模拟器而没有错误
firebase emulators:start --project demo-<PROJECT_ID>
用我的实际项目 ID 和本地目录替换
终端输出:
i emulators: Starting emulators: auth, functions, firestore, hosting, storage
i emulators: Detected demo project ID <PROJECT_ID>, emulated services will use a demo configuration and attempts to access non-emulated services for this project will fail.
+ functions: Using node@14 from host.
i firestore: Firestore Emulator logging to firestore-debug.log
i hosting: Serving hosting files from: build
+ hosting: Local server: http://localhost:5000
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "<LOCAL_DIR>" for Cloud Functions...
+ functions[us-central1-makeUppercase]: firestore function initialized.
+ functions[us-central1-onCreateEntry]: firestore function initialized.
┌─────────────────────────────────────────────────────────────┐
│ ✔ 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 │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Hosting │ localhost:5000 │ n/a │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Storage │ localhost:9199 │ http://localhost:4000/storage │
└────────────────┴────────────────┴─────────────────────────────────┘
Emulator Hub running at localhost:4400
Other reserved ports: 4500
但是,当我尝试通过托管在本地模拟器中的应用程序创建新的 Firestore 文档时,我的 Firestore 本地模拟器中没有显示任何内容。我的浏览器控制台也没有出现任何错误。
我能够手动创建新文档并在 Firestore 本地模拟器中与我的 Cloud Functions 本地模拟器进行通信。没问题。只有当我尝试通过我的应用与 Firestore 通信时才会遇到问题。
我错过了什么?非常感谢您的帮助!
【问题讨论】:
标签: firebase google-cloud-firestore google-cloud-functions firebase-hosting