简体   繁体   English

我无法让Firebase参与我的React项目

[英]I can't get Firebase to work on my React project

I am trying to get firebase to work on my React project (using Webpack v3), but I am having some issues. 我正在尝试使Firebase在我的React项目上工作(使用Webpack v3),但是遇到了一些问题。

I added "firebase": "^4.10.1" to my package.json and in a firebase.js file I added this: 我在package.json添加了"firebase": "^4.10.1" ,并在firebase.js文件中添加了以下内容:

import * as firebase from 'firebase';

const config = {
    apiKey: "API_KEY",
    authDomain: "DOMAIN",
    databaseURL: "DATA_URL",
    projectId: "ID",
    storageBucket: "STORAGE_BUCKET",
    messagingSenderId: "SENDER_ID"
};

firebase.initializeApp(config);

firebase.database().ref().set({
    message: 'Connection Successful!'
});

On my app.js I imported that firebase.js file. 在我的app.js我导入了该firebase.js文件。 import './firebase/firebase';

If I am not mistaking I am supposed to get the "Connection Successful!" 如果我没有记错的话,我应该获得“连接成功!” message over in my database, but it's not working. 消息在我的数据库中结束,但无法正常工作。

Instead I get a console error . 相反,我收到控制台错误 I looked around online and found a few people with similar errors saying that adding: 我在网上四处张望,发现有类似错误的人说:

node: {
console: true,
fs: 'empty',
child_process: 'empty',
net: 'empty',
tls: 'empty',
dgram: 'empty',
dns: 'empty',
}

to my webpack.config.json would fix it. 到我的webpack.config.json可以修复它。 I tried it and id didn't really work. 我尝试了,id并没有真正起作用。

I didn't get those errors any more, but instead I get Uncaught TypeError: util.inherits is not a function (full error here ). 我再也没有得到那些错误,而是得到了Uncaught TypeError: util.inherits is not a function此处 Uncaught TypeError: util.inherits is not a function完全错误)。 I kept looking and found this on the firebase documentation where they use var firebase = require("firebase/app"); 我一直在寻找,发现对他们使用火力文档var firebase = require("firebase/app"); instead of import . 代替import I tried it and still didn't work. 我试过了,但还是没用。

I've tried to yarn add util , but to no avail. 我试过yarn add util ,但无济于事。

Any idea why this is happening and /or how to fix it? 知道为什么会这样和/或如何解决? Thanks. 谢谢。

I use Firebase in React this way: 我以这种方式在Firebase中使用Firebase:

import firebase from 'firebase'
var config = {
    ...
};
var fire = firebase.initializeApp(config);
fire.database().ref()...
  1. import firebase and not import * as firebase , don't know what Firebase module do export, but maybe this change the things; import firebase而不import * as firebase ,不知道导出的Firebase模块是什么,但这也许会改变事情;
  2. I init a variabile to which I assign the result of Firebase initialization ( var fire = firebase.initalize... ), then I use that to make calls to db, storage and whatever. 我初始化一个变量,将Firebase初始化的结果分配给该变量( var fire = firebase.initalize... ),然后使用该变量来调用db,存储等。

To me, it works flawlessly, don't know if it could be due to different versions or if it's just the code. 对我来说,它可以完美地工作,不知道它是由于版本不同还是仅仅是代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM