【问题标题】:It looks like you're using the development build of the Firebase JS SDK in React App & Invalid API key看起来您在 React App & Invalid API key 中使用 Firebase JS SDK 的开发版本
【发布时间】:2021-05-03 10:58:23
【问题描述】:

我创建了一个 React 应用并在其上使用 firestore。根据此文档,我已尝试将 firebase 身份验证 集成到我的应用中以授权登录:https://firebase.google.com/docs/database/web/start

firebase.js:

import firebase from "firebase";
import "firebase/firestore";
import "firebase/auth";

const firebaseConfig = {
  apiKey: ***,
  authDomain: ***,
  projectId: ***,
  storageBucket: ***,
  messagingSenderId: ***,
  appId: ***,
  measurementId: ***,
};

firebase.initializeApp(firebaseConfig);
export const db = firebase.firestore();

我有一个名为 AuthContext.js 的上下文文件:

import React, { useState, useEffect, createContext } from "react";
import { authentication } from "./firestore";

export const AuthContext = createContext();
export const AuthProvider = ({ children }) => {
  const [currentUser, setCurrentUser] = useState(null);

  useEffect(() => {
    authentication.onAuthStateChanged((user) => {
      setCurrentUser(user);
    });
  }, []);

  return (
    <AuthContext.Provider value={{ currentUser }}>
      {children}
    </AuthContext.Provider>
  );
};

这是 App.js :

import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Nav from "./pages/Routes/Nav";
import Footer from "./pages/Routes/Footer";
import Home from "./pages/Home";
import Search from "./pages/Search";
import Detail from "./pages/Detail";
import User from "./pages/User";
import LoginSignup from "./pages/LoginSignup";
import { AuthProvider } from "./utility/AuthContext";

function App() {
  return (
    <AuthProvider>
      <Router>
        <div>
          <Nav />
          <Switch>
            <Route path="/" exact>
              <Home />
            </Route>
            <Route path="/search">
              <Search />
            </Route>
            <Route path="/detail/:id">
              <Detail />
            </Route>
            <Route path="/user">
              <User />
            </Route>
            <Route path="/LoginSignup">
              <LoginSignup />
            </Route>
          </Switch>
          <Footer />
        </div>
      </Router>
    </AuthProvider>
  );
}

export default App;

使用此配置,我收到此错误:

"It looks like you're using the development build of the Firebase JS SDK.
When deploying Firebase apps to production, it is advisable to only import
the individual SDK components you intend to use.

For the module builds, these are available in the following manner
(replace <PACKAGE> with the name of a component - i.e. auth, database, etc):

CommonJS Modules:
const firebase = require('firebase/app');
require('firebase/<PACKAGE>');

ES Modules:
import firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

Typescript:
import firebase from 'firebase/app';
import 'firebase/<PACKAGE>';"

还有:

Uncaught t {code: "auth/invalid-api-key", message: "Your API key is invalid, please check your copy it correct.", a: null}

我已尝试将这些行添加到 firebase.js:

import firebase from "firebase/app"

我研究过 Google 和 Stackoverflow,但无法解决。我绝对确定 API 密钥是正确的。我一遍又一遍地检查它。我想我错过了什么。

这是错误的屏幕截图:

在这些方面,我错过了什么?

【问题讨论】:

  • 要解决第一个问题,请使用import firebase from "firebase/app";,如果您使用search for the error message,也会在许多搜索结果中显示。我建议先解决这个问题,然后将其从问题中删除,以便人们可以专注于第二个问题。

标签: javascript reactjs firebase google-cloud-firestore firebase-authentication


【解决方案1】:

交换import firebase from "firebase";。使用import firebase from "firebase/app"; 摆脱开发构建警告。确保删除原始行,否则错误不会消失。

要修复 ID,请尝试转到 https://console.firebase.google.com/u/0/project/[YOUR_PROJECT_ID]/settings/general/

在那里你应该有一个正确预填充的firebaseConfig,你可以复制粘贴。

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2023-02-07
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多