javascript - Unable data in firebase - Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https:<YOUR FIR

I am unable to save data to firebase from a news api. I can fetch successfully but when I add my save f

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

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/project/yara-96b67/overview' }); 
const db = admin.firestore(); 
module.exports = { admin, db };

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.

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./project/yara-96b67/overview' }); 
const db = admin.firestore(); 
module.exports = { admin, db };
Share Improve this question edited Feb 25, 2020 at 14:30 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges asked Feb 25, 2020 at 10:53 biggest_boybiggest_boy 4816 silver badges24 bronze badges 3
  • 1 Presumably the URL you're using is wrong then? My Firebase is rusty but I'd guess that's where you set up the admin object, the initializeApp call, which you haven't shown us. – Rup Commented Feb 25, 2020 at 10:56
  • const admin = require('firebase-admin'); var serviceAccount = require('../serviceaccount.json'); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: 'console.firebase.google./project/yara-96b67/overview' }); const db = admin.firestore(); module.exports = { admin, db }; – biggest_boy Commented Feb 25, 2020 at 11:05
  • Thanks - please edit that into the question. The databaseURL there is the one it wants you to change. – Rup Commented Feb 25, 2020 at 11:07
Add a ment  | 

4 Answers 4

Reset to default 5

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.
firebase.initializeApp({
  apiKey: "AIza....",                             // Auth / General Use
  authDomain: "YOUR_APP.firebaseapp.",         // Auth with popup/redirect
  databaseURL: "https://YOUR_APP.firebaseio.", // Realtime Database
  storageBucket: "YOUR_APP.appspot.",          // 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. in the end.

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

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./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./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./firebasejs/8.4.3/firebase-analytics.js"></script>

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

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 mand 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.',
    databaseURL: 'https://your-database-id.firebasedatabase.app', // IMPORTANT!
    storageBucket: 'project-id.appspot.',
    measurementId: '...',
);

// ...

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.


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. :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742285178a4415193.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信