biggest_boy
biggest_boy

Reputation: 481

Unable data in firebase - Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com

I am unable to save data to firebase from a news api. I can fetch successfully but when I add my save function it returns this error:

Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com

See below my code:

    exports.getArticles = functions.https.onRequest((req, res) => {
    return request(newsURL)
        .then(data => save(data))
        .then(data => response(res, data, 201))
 });

 function request(url) {
    return new Promise(function (fulfill, reject) {
        client.get(url, function (data, response) {
            fulfill(data)
        })
    })
 }

 function response(res, data, code) {
    return Promise.resolve(res.status(code)
        .type('application/json')
        .send(data))
 }

 function save(data) {
    return admin.database().ref('/feed/news')
        .set({ data: data })
        .then(() => {
            return Promise.resolve(data);
        })
 }

const admin = require('firebase-admin'); 
var serviceAccount = require('../serviceaccount.json'); 
admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: 'console.firebase.google.com/project/yara-96b67/overview' }); 
const db = admin.firestore(); 
module.exports = { admin, db };

Upvotes: 0

Views: 5111

Answers (4)

Cheng Qing Low
Cheng Qing Low

Reputation: 91

For anyone who ran into this error while using Firebase with Flutter project:

If you have used $ flutterfire configure to configure firebase to your flutter project as shown in here before adding Realtime Database, you will have to run the command again to reconfigure your project or add "databaseURL" to the FirebaseOptions in your lib/firebase_options.dart.

Your lib/firebase_options.dart should look something like this:

// inside lib/firebase_options.dart

// ...

static const FirebaseOptions web = FirebaseOptions(
    apiKey: '...',
    appId: '...',
    messagingSenderId: '...',
    projectId: 'project-id',
    authDomain: 'project-id.firebaseapp.com',
    databaseURL: 'https://your-database-id.firebasedatabase.app', // IMPORTANT!
    storageBucket: 'project-id.appspot.com',
    measurementId: '...',
);

// ...

Upvotes: 1

liquidki
liquidki

Reputation: 1294

I ran into this error when setting up a project in the EU. In my case it was due to following a tutorial that hadn't been updated in a year or two and was using an older version of the Firebase SDK. The solution was to update my CDN links as listed here:

https://firebase.google.com/docs/web/setup?hl=en#add-sdks-initialize

Contents as of this writing:

<body>
  <!-- Insert these scripts at the bottom of the HTML, but before you use any Firebase services -->

  <!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/8.4.3/firebase-app.js"></script>

  <!-- If you enabled Analytics in your project, add the Firebase SDK for Analytics -->
  <script src="https://www.gstatic.com/firebasejs/8.4.3/firebase-analytics.js"></script>

  <!-- Add Firebase products that you want to use -->
  <script src="https://www.gstatic.com/firebasejs/8.4.3/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.4.3/firebase-firestore.js"></script>
</body>

Upvotes: 3

ve1ikiy
ve1ikiy

Reputation: 181

For any new folks reading this question. Google creates databaseURL differently for US and EU datacenters.

for Europe it's ... firebasedatabase.app

for US it's ... firebaseio.com


So, when on database creation step, Google asks you where database should be located? -> don't select EU

At least not until all middleware developers update their code to support different URL pattern in Firebase Config. :)

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80924

Inside the initializeApp, you need to use the following:

// Initialize default app
// Retrieve your own options values by adding a web app on
// https://console.firebase.google.com
firebase.initializeApp({
  apiKey: "AIza....",                             // Auth / General Use
  authDomain: "YOUR_APP.firebaseapp.com",         // Auth with popup/redirect
  databaseURL: "https://YOUR_APP.firebaseio.com", // Realtime Database
  storageBucket: "YOUR_APP.appspot.com",          // Storage
  messagingSenderId: "123456789"                  // Cloud Messaging
});

The above is a sample from the documentation, but the property databaseUrl, should take the url of the realtime database that contains ..firebaseio.com in the end.

You can find the url by entering the firebase console and going to the database section.

Upvotes: 5

Related Questions