[英]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()...
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模块是什么,但这也许会改变事情; var fire = firebase.initalize...
), then I use that to make calls to db, storage and whatever. 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.