admin管理员组文章数量:1333612
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
4 Answers
Reset to default 5Inside 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. :)
本文标签:
版权声明:本文标题:javascript - Unable data in firebase - Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https:<YOUR FIR 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742285178a2446800.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论