【发布时间】:2019-10-15 19:10:11
【问题描述】:
我正在尝试使用 Firebase 检查用户是否在我的 React 网页上登录,但是我不断收到错误 TypeError: Cannot read property 'onAuthStateChanged' of undefined,它链接到的代码行如下:
登录.js:
authListener() {
Fire.auth.onAuthStateChanged((user) => {
if (user) {
this.setState({ user })
} else {
this.setState({ user: null })
}
})}
Fire.js:
import firebase from 'firebase'
const config = {
apiKey: "xxx",
authDomain: "xxx",
databaseURL: "xxx",
projectId: "xxx",
storageBucket: "xxx",
messagingSenderId: "xxx",
appId: "xxx"
};
const Fire = firebase.initializeApp(config);
export default Fire;
** 解决了,上面是我的 Fire.js 和 Login.js 对于那些遇到同样问题的人的样子**
【问题讨论】:
-
我觉得应该是
Fire.auth().onAuthStateChanged而不是Fire.auth.onAuthStateChanged -
我试过了,它返回
TypeError: _Fire__WEBPACK_IMPORTED_MODULE_1__.default.auth is not a function -
Fire持有什么?你从哪里进口的?可以分享fire.js文件的代码吗? -
我编辑了我的原始帖子以显示 Fire.js 中的内容
标签: javascript reactjs firebase firebase-authentication