对不起标题,我不确定这是否是我面临的正确问题,但我会尝试描述我正在尝试做的事情。
我已经更新了标题,因为其中的错误似乎是我的问题(原文:“Emulated firstore not Saving documents?”)。
我有一个在产品上使用 Firebase 的 React 应用程序(功能、身份验证、firestore 和存储),我需要在那里更改一些东西,所以我认为先在本地进行这些更改然后部署它们会更安全。因此,我设置了 firebase 模拟器套件并设置应用程序的环境变量以指向本地内容(应用程序的这一部分不需要存储,因此本地未启用)。
无论如何,我设置了一个本地身份验证帐户,以便我可以登录该帐户并将文档添加到“用户”集合(这是必需的,因为应用程序在 firestore 中查找登录用户的文档)但是,似乎 firestore当我刷新模拟器 UI 时,模拟器摆脱了这个文档?
你可以在这个 GIF 中看到我的意思:Firestore getting rid of the document
可能需要的其他信息:
❯ firebase emulators:start --import ./functions/firestore --export-on-exit
i emulators: Starting emulators: auth, functions, firestore
! functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: database, hosting, pubsub, storage
+ functions: Using node@12 from host.
i firestore: Importing data from E:\Project\api\functions\firestore\firestore_export\firestore_export.overall_export_metadata
! firestore: Did not find a Cloud Firestore rules file specified in a firebase.json config file.
! firestore: The emulator will default to allowing all reads and writes. Learn more about this option: https://firebase.google.com/docs/emulator-suite/install_and_configure#security_rules_configuration.
i firestore: Firestore Emulator logging to firestore-debug.log
i auth: Importing config from E:\Project\api\functions\firestore\auth_export\config.json
i auth: Importing accounts from E:\Project\api\functions\firestore\auth_export\accounts.json
! ui: Emulator UI unable to start on port 4000, starting on 4001 instead.
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "E:\Project\api\functions" for Cloud Functions...
+ functions[us-central1-auth]: http function initialized (http://localhost:5001/my-project/us-central1/auth).
i functions[us-central1-backup-scheduledFirestoreExport]: function ignored because the pubsub emulator does not exist or is not running.
+ functions[us-central1-makeAdmin]: http function initialized (http://localhost:5001/my-project/us-central1/makeAdmin).
+ functions[us-central1-users]: http function initialized (http://localhost:5001/my-project/us-central1/users).
┌─────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! It is now safe to connect your app. │
│ i View Emulator UI at http://localhost:4001 │
└─────────────────────────────────────────────────────────────┘
┌────────────────┬────────────────┬─────────────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Authentication │ localhost:9099 │ http://localhost:4001/auth │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5001 │ http://localhost:4001/functions │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ localhost:8080 │ http://localhost:4001/firestore │
└────────────────┴────────────────┴─────────────────────────────────┘
Emulator Hub running at localhost:4400
Other reserved ports: 4500
Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
我觉得这是阻止我的问题,因为应用程序在控制台中出现错误[2021-06-22T09:58:16.805Z] @firebase/firestore: Firestore (8.2.4): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
这是由线路引起的await firestore().doc(`users/${loggedInUser.uid}`).get();
我猜这是因为不存在具有该 UID 的用户文档(因为我似乎无法创建它)。应用程序的环境和 firebase 初始化:
REACT_APP_FIREBASE_AUTH_DOMAIN=http://localhost:9099
REACT_APP_FIREBASE_DATABASE_URL=http://localhost:8080
REACT_APP_FIREBASE_PROJECT_ID=my-project
REACT_APP_FIREBASE_STORAGE_BUCKET=my-project.appspot.com
REACT_APP_FIREBASE_FUNCTIONS_ENDPOINT=http://localhost:5001/my-project/us-central1
-----
if (process.env.NODE_ENV !== 'production') {
console.log("USING LOCAL EMULATORS");
firebase.auth().useEmulator(process.env.REACT_APP_FIREBASE_AUTH_DOMAIN);
firebase.firestore().useEmulator('localhost', 8080);
firebase.functions().useEmulator('localhost', 5001);
firebase.firestore.setLogLevel('debug');
}
node -v
:v12.0.0(这是产品正在运行的)firebase --version
: 9.13.1任何帮助,将不胜感激。
编辑:即使通过函数添加用户也不起作用:
const userDoc = await admin
.auth()
.createUser({
email: safeUser.email,
password: safeUser.password,
displayName: `${safeUser.firstName} ${safeUser.lastName}`,
});
delete safeUser.password;
await admin.firestore().collection('users').doc(userDoc.uid).set(safeUser);
res.status(200).send(JSON.stringify({ uid: userDoc.uid, ...safeUser }));
哪个确实返回了用户{
"data": {
"uid": "owLduAuMku520XVVm5J6qq92DPZp",
"email": "test@test.com",
"firstName": "Test",
"lastName": "Account",
"dob": "1900-01-01T00:00:00.000Z",
"phoneNumber": "+447999999999"
},
"status": 200,
"statusText": "OK",
"headers": {
"content-length": "224",
"content-type": "text/html; charset=utf-8"
},
"config": {
"url": "http://localhost:5001/my-project/us-central1/createUser",
"method": "post",
"data": "{\"consent\":true,\"phoneNumber\":\"+447999999999\",\"dob\":\"1900-01-01T00:00:00.000Z\",\"password\":\"password\",\"email\":\"test@test.com\",\"firstName\":\"Test\",\"lastName\":\"Account\"}",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8"
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1
},
"request": {}
}
但是,它仍然没有显示在模拟器 UI 中(它创建了 auth 用户)并且日志是正常的:i functions: Beginning execution of "us-central1-createUser"
! Received service account token ---- Assuming that it owns project "my-project".
! Received service account token ---- Assuming that it owns project "my-project".
i functions: Finished "us-central1-createUser" in ~2s
编辑 2:在模拟器 UI 上打开控制台,在加载用户集合时,我看到了与应用程序相同的错误:[2021-06-22T11:44:56.597Z] @firebase/firestore: Firestore (8.5.0): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
编辑 3:似乎连接到端口 8080 的 url 暂时解决了这个问题。转到单独选项卡中的 channel URL,然后允许 firebase 从中加载数据......更改端口会解决问题吗?编辑 4:更改端口确实 不是 解决问题。似乎是下面的 URL 导致了问题:
http://localhost:8081/google.firestore.v1.Firestore/Listen/channel?database=projects%2Fmy-project%2Fdatabases%2F(default)&VER=8&RID=rpc&SID=FgqppSIdbuIaPmABDJD6fA%3D%3D&CI=0&AID=0&TYPE=xmlhttp&zx=yzaslurbjssn&t=1
编辑 5:更新到最新的 firebase 模块没有帮助
最佳答案
好的,所以经过大量研究并环顾四周,似乎添加 firebase.firestore().settings({ experimentalAutoDetectLongPolling: true });
修复了应用程序上的问题。它不能解决 UI 的问题(我希望它能够工作,所以我可以看到正在发生的事情)但是,我想我暂时可以忍受。
对于其他有同样问题的人,这是我的完整 firebase 配置:
if (process.env.NODE_ENV !== 'production') {
console.log("USING LOCAL EMULATORS");
firebase.auth().useEmulator(process.env.REACT_APP_FIREBASE_AUTH_DOMAIN);
firebase.firestore().useEmulator('localhost', 8081);
firebase.firestore().settings({ experimentalAutoDetectLongPolling: true }); //This seems to fix an issue with firestore emulator...
var storage = firebase.storage();
console.log("storage", storage);
storage.useEmulator("localhost", 9199);
firebase.functions().useEmulator('localhost', 5001);
firebase.firestore.setLogLevel('debug');
}
如果有人知道为什么会这样,或者如何让模拟器 UI 工作,请告诉。我还在寻找答案!编辑:如果其他人在 Windows 上遇到同样的问题,请尝试 Linux!似乎在那里工作得很好!
关于javascript - 模拟 firstore : Firestore (8. 5.0):无法访问 Cloud Firestore 后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68081809/