简体   繁体   English

使用 Firestore 模拟器时无法从客户端应用写入 Firestore

[英]Cannot write to Firestore from client app when using Firestore Emulator

I'm having problems creating documents in Firestore while using Firestore Emulator.我在使用 Firestore 模拟器时在 Firestore 中创建文档时遇到问题。 Below is the error I see in logs:以下是我在日志中看到的错误:

W/Firestore(13200): (24.0.1) [WriteStream]: (aa74142) Stream closed with status: Status{code=UNKNOWN, description=null, cause=null}. W/Firestore(13200): (24.0.1) [WriteStream]: (aa74142) Stream 关闭状态:Status{code=UNKNOWN, description=null, cause=null}。

In my app, after user signs up, I check if there is a document related to this user.在我的应用程序中,用户注册后,我检查是否有与该用户相关的文档。 This read operation succeeds, therefore I conclude that I can read from Firestore.此读取操作成功,因此我得出结论,我可以从 Firestore 读取。 If there isn't a document related to the user, a document gets created.如果没有与用户相关的文档,则会创建一个文档。 This is the point that the code gets stuck.这就是代码卡住的地方。 The write operation does not return.写操作不返回。 It does not throw an exception either.它也不会抛出异常。

I'm using both Authentication and Firestore emulators.我同时使用身份验证和 Firestore 模拟器。

This problem does not occur all the time.此问题不会一直发生。 For example in the app, when a new FCM token has been generated, the token is saved into the Firestore and this works without any issues.例如,在应用程序中,当生成新的 FCM 令牌时,该令牌将保存到 Firestore 中,并且可以正常工作。 I can see the document in Firestore Emulator Dashboard.我可以在 Firestore 模拟器仪表板中看到该文档。 I can also create documents manually using the dashboard.我还可以使用仪表板手动创建文档。

The problem occurs both on physical device and Android Emulator.该问题同时出现在物理设备和 Android 模拟器上。 While testing on physical device, I connect to the Firestore Emulator by specifying the IP address of my computer which the Firestore Emulator is running on.在物理设备上进行测试时,我通过指定运行 Firestore 模拟器的计算机的 IP 地址连接到 Firestore 模拟器。 On Android Emulator, I provide localhost as the host of the Firestore Emulator.在 Android 模拟器上,我提供localhost作为 Firestore 模拟器的主机。

I've also created an issue in flutterfire repository.我还在 flutterfire 存储库中创建了一个问题

Output of flutter doctor : Output flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.10.1, on Pop!_OS 21.10 5.15.23-76051523-generic, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0-rc1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.64.2)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

• No issues found!

Versions of cloud_firestore and firebase_core packages: cloud_firestorefirebase_core包的版本:

cloud_firestore: ^3.1.8
firebase_core: ^1.12.0

My problem with the emulator suite was the Kaspersky antivirus.我的模拟器套件问题是卡巴斯基防病毒软件。 After disabling it, everything works fine.禁用它后,一切正常。 Maybe this help you!也许这对你有帮助!

This happens to me as well and never has before.这也发生在我身上,以前从未有过。 The bug I experience is when I create a document using the Android emulator it's like I can't write to it from any other source.我遇到的错误是,当我使用 Android 模拟器创建文档时,我无法从任何其他来源写入文档。 So for example when testing I'll be using separate accounts in my app in the Android emulator, Firefox, and on web for my Android phone.因此,例如,在测试时,我将在 Android 模拟器、Firefox 和 web 的 Android 手机的应用程序中使用单独的帐户。 If the doc I'm trying to write to was created by the emulator, I can't write to it from either web on my phone or my computer's browser.如果我尝试写入的文档是由模拟器创建的,则我无法通过手机或计算机浏览器上的 web 写入。 If I use the Chrome emulator instead everything works fine.如果我改用 Chrome 模拟器,一切正常。 It also works fine if I create the doc initially on my phone or browser then write to it from the android emulator/web/web for android phone.如果我最初在手机或浏览器上创建文档,然后从 android 手机的 android 模拟器/web/web 写入它,它也可以正常工作。 Obviously it's really hard to debug since it only happens while creating the initial doc from the Android emulator.显然很难调试,因为它只会在从 Android 模拟器创建初始文档时发生。

Edit: After hooking up the debugger to Chrome and then creating the raid doc in the Android emulator I was able to fix my problem.编辑:将调试器连接到 Chrome,然后在 Android 模拟器中创建 raid 文档后,我能够解决我的问题。 I believe there is a recent change to the way the Android emulator handles timestamps that is creating my error.我相信 Android 模拟器处理时间戳的方式最近发生了变化,这导致了我的错误。

This is what a timestamp stored in Firebase by the Android emulator looks like using Timestamp.fromDate(DateTime.now());: Timestamp(seconds=1646095038, nanoseconds=780498000)这是 Firebase 模拟器使用 Timestamp.fromDate(DateTime.now()); 时存储在 Firebase 中的时间戳:Timestamp(seconds=1646095038, nanoseconds=780498000)

In Chrome the same command looks like this: Timestamp(seconds=1646095976, nanoseconds=800000000) Chrome在 Chrome 中,相同的命令如下所示:Timestamp(seconds=1646095976, nanoseconds=800000000) Chrome

What is happening in the last week is this chunk of code designed to get the most recent raid doc in my MMORPG app is failing to get a result from firebase on web when the timestamp is created in the Android emulator:上周发生的事情是,当在 Android 模拟器中创建时间戳时,这段旨在在我的 MMORPG 应用程序中获取最新 raid 文档的代码无法从 firebase 在 web 上获得结果:

   //update most recent raid doc
  Future<void> updateRaid(String guildName, Raid raid) async {
    var snap = await _db
        .collection('guilds')
        .doc(guildName)
        .collection('raids')
        .where('raidDocCreated', isEqualTo: raid.raidDocCreated)
        .get();
    var docId = snap.docs[0].id;
    return _db
        .collection('guilds')
        .doc(guildName)
        .collection('raids')
        .doc(docId)
        .set(raid.toJson());
  }

Bottom line is if you are doing something similar to this where you are trying to query a timestamp in firebase stored by the Android emulator, web will not compare equality accurately.底线是,如果您正在做与此类似的事情,您正在尝试查询 Android 模拟器存储的 firebase 中的时间戳,则 web 将无法准确比较相等性。 I was able to fix this problem by storing an int in the database to query the most recently created doc by instead of a Timestamp using DateTime.now().millisecondsSinceEpoch.我能够通过在数据库中存储一个 int 来解决这个问题,而不是使用 DateTime.now().millisecondsSinceEpoch 的时间戳来查询最近创建的文档。 Now when my doc gets created on the Android emulator, web can properly compare the equality of the raidDocCreatedAt variable.现在,当我的文档在 Android 模拟器上创建时,web 可以正确比较 raidDocCreatedAt 变量的相等性。

The issue I was having has been resolved.我遇到的问题已经解决。 I don't know which change was behind this but I remember making several upgrades in the environment that I run the Firebase Emulators.我不知道这背后的变化是什么,但我记得在我运行 Firebase 模拟器的环境中进行了几次升级。 It might as well be changing the version of node or upgrading Firebase Admin SDK.它也可能是更改节点的版本或升级 Firebase Admin SDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Firebase模拟器写入Firestore、Storage? - How to use Firebase Emulator to write to Firestore, Storage? 为什么我的 iOS 应用程序没有写入 firestore 模拟器 - Why is my iOS app not writing to the firestore emulator 使用服务器端 firestore 客户端时是否可以选择乐观锁定? - Is optimistic locking an option when using a server-side firestore client? 从 flutter 应用程序在 firestore 中更新数据后无法读取数据 - Cannot read data after updating it in firestore from flutter app 从 google firestore -Quiz 应用程序获取问题时应用程序崩溃 - App crashes when fetch questions from google firestore -Quiz app Firebase android 中与 firestore SDK 一起使用时的应用程序检查配额限制 - Firebase App Check Quota Limits when using with firestore SDK in android 无法使用 cypress 使用 Firestore 本地模拟器测试应用程序 - can't use cypress to test app using using firestore local emulator 如何使用 Firestore 从 flutter 应用程序操作 SQL 服务器 - How to manipulate SQL server from flutter app by using Firestore 具有匿名凭据的 Firestore 模拟器 - Firestore emulator with anonymous credentials 使用 Python 从 Firestore 加载文档时出现“错误的应用程序引用” - "Wrong app reference" when loading documents from Firestore with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM