1

I'm trying to test my Android app, which uses data from Firebase Firestore. However, I have an issue when querying data using my local running Firebase Firestore Emulator and the Android Emulator.

When I query data with the Android Emulator from the Cloud Firestore everything works fine. When I query data from the Emulator Firestore I receive an empty DataSnapshot.

Both Firestore instances (local emulator and cloud Firestore) contain the same data.

When I enter 10.0.2.2.:4001 inside the browser of my Android Emulator I come to the Firebase Emulator Suite and can see that the Firestore emulator status is On. In my opinion this means that a connection to my locally running Firestore emulator exists.

My code to init the Firestore instance:

private val db = Firebase.firestore
db.useEmulator("10.0.2.2", 8080)
db.firestoreSettings = firestoreSettings { isPersistenceEnabled = false }
val docVal = db.collection("district_codes")
            .document("district_codes")
            .get()
            .await()

Logs of my running local Firestore emulator:

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://127.0.0.1:4001/               │
└─────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ 127.0.0.1:8080 │ http://127.0.0.1:4001/firestore │
└───────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at 127.0.0.1:4401
  Other reserved ports: 4501, 9150

Why am I unable to retrieve data from my locally running Firebase Firestore instance, while data retrieval from the Cloud Firestore instance works correctly?

Update I try to test it with a simple test entry: In the path /Test/doc_test, I added a string field named test_field.

The structure of my test entry on my local Firestore emulator: enter image description here

I retrieve it using the following command:

val docVal = db.collection("Test").document("doc_test").get().await()
val foundValue = docVal.get("test_field") as String?
Log.i(TAG_FIRESTORE, "Found following test value: ${foundValue} - ${docVal}")

And get following Log-entry:

Found following test value: null - DocumentSnapshot{key=Test/doc_test, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=null}

6
  • The code seems to work as expected, so the first thing I'd consider is the data. How did you enter the data into the emulator? And how did you verify that it matches your code (e.g. no extra spaces, typos, etc)? Commented Dec 15, 2024 at 16:27
  • The data was loaded from Cloud Firestore directly into the local Firestore emulator using Python. I also test it with a simple database entry (see updated question). Commented Dec 15, 2024 at 19:49
  • That log looks like it's loading an existing-but-empty document Commented Dec 15, 2024 at 22:29
  • The document is not empty. I have put a screenshot of the small test db to the question. Commented Dec 16, 2024 at 7:09
  • Hmm... I have no idea what's going wrong then. Hopefully somebody else spots it. Commented Dec 16, 2024 at 14:24

1 Answer 1

0

The issue was caused by using the wrong project ID. In the Firestore-debug.log file, the following entry highlighted the problem:

Multiple projectIds are not recommended in single project mode. Requested project ID <my_project>, but the emulator is configured for <my_project>_testing.

This occurred because I was using a different project ID for the emulator than the one configured for Firebase Firestore in the cloud.

To resolve the issue, I updated the project ID in the emulator configuration to match the one used in Firebase Firestore.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.