代码之家  ›  专栏  ›  技术社区  ›  Alexander Solonik

使用带有firebase的axios,我需要从npm安装firebase吗?

  •  0
  • Alexander Solonik  · 技术社区  · 8 年前

    我在我的项目中安装了axios,

    在我的 src/ 文件夹已创建axios.js公司看起来像这样的文件:

    import axios from 'axios';
    
    const instance = axios.create({
        baseURL : 'https://myprojectname-11651.firebaseio.com/'
    });
    
    export default instance;
    

    import axios from '../axios.js';
    
    // removing code thats not necessary for this example
    
    componentDidMount() {
            // alert();
    
            axios.get('/habits/badHabits.json')
                .then( (resp) => {
                    console.log(resp);
                })
    
        }
    

    我的控制台出现401错误,firebase的仪表板似乎一年前就已经改变了,我也不能像一年前那样在firebase上使用axios。

    我的数据库看起来是这样的:

    enter image description here

    2 回复  |  直到 8 年前
        1
  •  2
  •   Renaud Tarnec    8 年前

    我感觉你把实时数据库REST API和Firestore API搞混了。

    从你所问的图片来看,你在使用Firestore。

    对于Firestore,“所有REST API端点都存在于基本URL下 https://firestore.googleapis.com/v1beta1/ “,见文件 here .

    另一方面 here ,实时DB REST API端点有一个类似于 https://docs-examples.firebaseio.com/rest/

        2
  •  1
  •   Pradhumn Sharma    8 年前

    是的,您必须使用npm在项目中安装firebase。这是使用firebase配置应用程序的初始过程。然后您必须添加firebase凭据,如

    <script>
      // Initialize Firebase
      var config = {
        apiKey: <YOUR_APP_KEY>,
        authDomain: "<YOUR_APP_DOMAIN>",
        databaseURL: "<YOUR_DATABASE_URL>",
        projectId: "<YOUR_PROJECT_ID>",
        storageBucket: "<YOUR_STORAGE_URL>",
        messagingSenderId: "<YOUR_MESSENGER_ID>"
      };
      firebase.initializeApp(config);
    </script>
    

    在结构:-你可以在这里找到上面的代码, Firebase控制台>项目设置(这是与“项目概述”文本相对的齿轮图标)>您的应用程序>(在同一页上,您有三种选择,可以将其用于android、ios、web)>将Firebase添加到您的web应用程序

    现在您已经添加了这些内容,您只需要使用firebase函数来访问firebase存储、身份验证和数据库。

    https://firebase.google.com/docs/web/setup

    它会帮助你的。