【问题标题】:Emulated firstore: Firestore (8.5.0): Could not reach Cloud Firestore backend模拟 firstore:Firestore (8.5.0):无法访问 Cloud Firestore 后端
【发布时间】:2021-09-05 22:40:34
【问题描述】:

对不起标题,我不确定这是否是我面临的正确问题,但我会尝试描述我正在尝试做的事情。

我已经更新了标题,因为其中的错误似乎是我的问题(原文:“Emulated firstore not Saving documents?”)。

我有一个在产品上使用 Firebase 的 React 应用程序(函数、身份验证、firestore 和存储),我需要在那里更改一些东西,所以我认为先在本地进行这些更改然后部署它们会更安全。因此,我设置了 firebase 模拟器套件,并将应用程序的环境变量设置为指向本地内容(应用程序的这一部分不需要存储,因此未在本地启用)。

无论如何,我设置了一个本地身份验证帐户,以便我可以登录该帐户并将文档添加到“用户”集合(这是必需的,因为应用程序在 firestore 中查找已登录用户的文档)但是,似乎当我刷新模拟器 UI 时,firestore 模拟器会删除此文档吗?

你可以在这个 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(prod 正在运行)

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 暂时解决了这个问题。转到单独选项卡中的频道 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 模块没有帮助

【问题讨论】:

    标签: javascript firebase google-cloud-firestore firebase-tools


    【解决方案1】:

    好的,经过 很多 更多的研究并环顾四周,似乎添加 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!似乎在那里工作得很好!

    【讨论】:

      猜你喜欢
      • 2019-02-07
      • 2020-09-29
      • 2018-09-15
      • 2021-08-23
      • 2020-10-21
      • 2021-08-13
      • 2022-06-15
      • 2018-06-29
      • 1970-01-01
      相关资源
      最近更新 更多