Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hosting emulator uses a different RTDB instance from Functions and Database emulators. #2965

Closed
bkeil opened this issue Dec 24, 2020 · 4 comments · Fixed by #2979
Closed

Comments

@bkeil
Copy link

bkeil commented Dec 24, 2020

[REQUIRED] Environment info

firebase-tools:
9.1.0

Platform: Raspbian
Linux raspberrypi 5.4.79-v7l+

[REQUIRED] Test case

functions/index.js

const functions = require('firebase-functions');
const admin = require("firebase-admin");

admin.initializeApp();

exports.addMessage = functions.https.onRequest(async (req, res) => {
    const original = req.query.text;
    const messageRef = await admin.database().ref('/messages').push();
    await messageRef.set({original: original});
    res.json({result: `Message with ID: ${messageRef.key} added.`});
  });

public/index.html
database-debug.log
firebase-debug.log
pubsub-debug.log
ui-debug.log

<html>
<head>
  <meta charset="utf-8">
  <title>DB Instance Name Bug</title>
  <script defer src="/__/firebase/8.2.1/firebase-app.js"></script>
  <script defer src="/__/firebase/8.2.1/firebase-database.js"></script>
  <script defer src="/__/firebase/init.js?useEmulator=true"></script>
</head>

<body>
  <script>
    document.addEventListener('DOMContentLoaded', function () {
      firebase.database().ref('/messages').once('value').then(
        (snapshot) => console.log(snapshot.val()),
        (error) => console.log(error)
      );
    });
  </script>
</body>
</html>

[REQUIRED] Steps to reproduce

  1. Create a new project in the Firebase console.
  2. Make a new local directory for the project with firebase init.
    A. Select the project from (1).
    B. Select database, hosting, emulators.
    C. Accept all defaults.
  3. Create public/index.html and functions/index.js as above
  4. Run firebase emulators:start
  5. Invoke the addMessage function: curl http://localhost:5001/*project-id*/us-central1/addMessage?text=test
  6. Load the hosted website: http://localhost:5000/

[REQUIRED] Expected behavior

All three emulators, Functions, Database, and Hosting should all refer to the same instance.

When the page is loaded in (6), an object with the message from (5) should be logged to the console.

I have verified that this project works as intended when deployed to production.

[REQUIRED] Actual behavior

The Functions emulator and the Database emulator both use http://localhost:9000?ns=*project-id*, but the Hosting emulator connects to ws://localhost:9000/.ws?v=5&ns=project-id-default-rtdb

The Hosting emulator uses a database with the "-default-rtdb" suffix, while the other two emulators do not use that suffix.

When the page is loaded in (6), a "null" is logged to the console.

After loading the page, the database emulator shows two databases, one with and one without the "-default-rtdb" suffix.

@bkeil
Copy link
Author

bkeil commented Dec 24, 2020

The debug logs ended up in the middle of the ticket somehow.... They're in the Test case section, repeated here:

database-debug.log
firebase-debug.log
pubsub-debug.log
ui-debug.log

@samtstern
Copy link
Contributor

samtstern commented Dec 28, 2020

@bkeil thank you for reporting this and for the excellent reproduction steps. I can confirm this issue and it's actually a problem in the Functions emulator, where we are making the assumption that your Database URL is https://<PROJECT-ID>.firebaseio.com which is no longer a safe assumption now that we no longer create a default namespace on project creation!

@bkeil
Copy link
Author

bkeil commented Dec 28, 2020

@samtstern Wow! That's amazing! Thank you for this fix!

@phcoliveira
Copy link

I do not think this issue was fixed. I am using:

"firebase-tools": "13.14.0",
"firebase-admin": "12.1.0",
"firebase-functions": "5.0.1"

And I always end up having at least two databases.
Here is my file .firebaserc

{
  "projects": {
    "default": "noice-app-eu-staging",
    "dev": "demo-dev"
  }
}

And this is the command I used:

pnpm exec firebase emulators:start --only=database --project=dev --debug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
  翻译: