reactjs 看起来您正在使用Firebase JS SDK的开发版本,将Firebase应用程序部署到生产环境时,

z31licg0  于 2023-04-20  发布在  React
关注(0)|答案(2)|浏览(131)

我正在尝试像这样将Firebase与ReactJs集成。
这是我的代码

import firebase from "firebase";


var firebaseConfig = {
  apiKey: "", // Add API Key
  databaseURL: "" // Add databaseURL
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

export const db = firebase;

var firepadRef = firebase.database().ref();

export const userName = localStorage.getItem('auth_name');
const urlparams = new URLSearchParams(window.location.search);
const roomId = urlparams.get("id");

if (roomId) {
  firepadRef = firepadRef.child(roomId);
} else {
  firepadRef = firepadRef.push();
}

export default firepadRef;

现在我得到了这个警告:
看起来您正在使用Firebase JS SDK的开发版本。将Firebase应用程序部署到生产环境时,建议仅导入您打算使用的单个SDK组件。
对于模块构建,这些可以通过以下方式获得(替换为组件的名称-即auth,数据库等):
通用JS模块:const firebase = require('firebase/app');require('firebase/');
ES模块:从'firebase/app'导入firebase;import 'firebase/';
Typescript:从'firebase'导入firebase

ru9i0ody

ru9i0ody1#

在一个新的firebase.initializeApp之后(假设你使用的是firebase的v9),你需要以不同的方式示例化数据库。检查你的文档。我会这样写你的代码:

// firebase file which you export the initialization
import firebase from "firebase";


var firebaseConfig = {
  apiKey: "", // Add API Key
  databaseURL: "" // Add databaseURL
};

// Initialize Firebase
var fire = firebase.initializeApp(firebaseConfig);
export fire.

//second js file
import firebase from ./firebase.js
import {getDatabase, ref, set} from "firebase/database";

var db = getDatabase(Firebase);

export const userName = localStorage.getItem('auth_name');
const urlparams = new URLSearchParams(window.location.search);
const roomId = urlparams.get("id");

//if you want to push to the db or something, you call the method
set(ref(db, 'users/' + userId), {
    username: name,
    email: email,
    profile_picture : imageUrl
  });

//more code here

总而言之,再检查一次文档,使用版本9,它的实现是最好的。https://firebase.google.com/docs/database/web/read-and-write#web-version-9_1

ryhaxcpt

ryhaxcpt2#

这对我来说很好。

import { initializeApp } from "firebase/app";
import "firebase/compat/auth";
import "firebase/compat/firestore";
import { child, getDatabase, push, ref } from "firebase/database";

const firebaseConfig = {
    apiKey: "", // Add API Key
    databaseURL: "" // Add databaseURL
};

const app = initializeApp(firebaseConfig);
export const db = getDatabase(app);

var firepadRef = ref(db);

export const userName = prompt("What's your name?");
const urlparams = new URLSearchParams(window.location.search);
const roomId = urlparams.get("id");

if (roomId) {
    firepadRef = child(firepadRef, roomId);
} else {
    firepadRef = push(firepadRef);
    window.history.replaceState(null, "Meet", "?id=" + firepadRef.key);
}

export default firepadRef;

相关问题