【问题标题】:Unable data in firebase - Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com无法在 firebase 中使用数据 - 错误:FIREBASE 致命错误:无法解析 Firebase url。请使用 https://<YOUR FIREBASE>.firebaseio.com
【发布时间】:2022-03-15 12:17:58
【问题描述】:

我无法从新闻 api 将数据保存到 firebase。我可以成功获取,但是当我添加我的保存功能时,它会返回此错误:

错误:FIREBASE 致命错误:无法解析 Firebase 网址。请使用https://<YOURFIREBASE>.firebaseio.com

见下面我的代码:

    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 };

【问题讨论】:

  • 想必您使用的 URL 是错误的?我的 Firebase 生锈了,但我猜这就是你设置管理对象的地方,initializeApp 调用,你没有向我们展示。
  • 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 };
  • 谢谢 - 请将其编辑到问题中。那里的 databaseURL 是它希望您更改的那个。

标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

initializeApp里面,你需要使用如下:

// 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
});

以上是来自documentation 的示例,但databaseUrl 属性应该取最后包含..firebaseio.com 的实时数据库的url。

您可以通过进入 firebase 控制台并转到数据库部分来找到该 url。

【讨论】:

    【解决方案2】:

    我在欧盟设立项目时遇到了这个错误。就我而言,这是由于遵循的教程在一两年内没有更新,并且使用的是旧版本的 Firebase SDK。解决方案是更新我的 CDN 链接,如下所示:

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

    撰写本文时的内容:

    <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>
    

    【讨论】:

    • 我的也可以使用这个版本
    【解决方案3】:

    对于阅读此问题的任何新人。 Google 为美国和欧盟数据中心创建不同的 databaseURL。

    对于欧洲,它是 ... firebasedatabase.app

    美国是 ... firebaseio.com


    所以,在创建数据库的步骤中,Google 会询问您数据库应该位于哪里? -> 不要选择欧盟

    至少在所有中间件开发人员更新他们的代码以支持 Firebase Config 中的不同 URL 模式之前。 :)

    【讨论】:

      【解决方案4】:

      对于在使用 Firebase 和 Flutter 项目时遇到此错误的任何人:

      如果您在添加实时数据库之前使用$ flutterfire configure 为您的flutter 项目配置了firebase,如here 所示,则必须再次运行该命令以重新配置您的项目或将“databaseURL”添加到您的FirebaseOptions lib/firebase_options.dart.

      您的lib/firebase_options.dart 应该如下所示:

      // 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: '...',
      );
      
      // ...
      

      【讨论】:

        猜你喜欢
        • 2021-11-07
        • 2021-04-19
        • 2019-05-24
        • 1970-01-01
        • 2016-03-08
        • 2018-03-15
        • 2020-08-20
        • 1970-01-01
        • 2019-05-08
        相关资源
        最近更新 更多