gpt4 book ai didi

javascript - 未捕获的 TypeError : (0 , _firebase.auth) 不是函数

转载 作者:行者123 更新时间:2023-12-03 02:33:57 25 4
gpt4 key购买 nike

尝试在 React 应用中使用 Firebase 身份验证。我按照 firbase 文档中的说明进行操作。

使用 npm 安装 firebase

添加了 firebase.js 文件:

import * as firebase from 'firebase'

var config = {
apiKey: "AIzaSyBUORmX2qG7BXFC4hqfKAq0Y6HMAsLTGbw",
authDomain: "hardy-brooklyn.firebaseapp.com",
databaseURL: "https://hardy-brooklyn.firebaseio.com",
projectId: "hardy-brooklyn",
storageBucket: "hardy-brooklyn.appspot.com",
messagingSenderId: "392580429077"
};
firebase.initializeApp(config);

const auth = firebase.auth();

export {
auth,
};

调用我的组件中的身份验证函数之一:

"use strict";
import React, { Component } from "react";
import { auth } from "../../firebase"

export default class Nav extends Component {
constructor(props) {
super(props);
this.state = {
nav: "hideNav",
login: "hideLogin",
email: "",
password: ""
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);

}

handleChange(event) {
this.setState({email: event.target.value});
}

handleSubmit(event) {
event.preventDefault();
let email = this.state.email;
let password = this.state.password;
auth().signInWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var isAnonymous = user.isAnonymous;
var uid = user.uid;
var providerData = user.providerData;
console.log(displayName)
} else {
console.log('no user')
}
});


}

render() {
return (
<div>
<form id="login" style={loginStyles[this.state.login]} onSubmit={this.handleSubmit}>
ADMIN USE ONLY
<br/>
Email:
<input type="email" name="email" onChange={this.handleChange}/>
<br/>
Password:
<input type="password" name="password"/>
<br/>
<button >submit</button>
<button onClick={this.hideLogin}>hide</button>
</form>
</div>
);
}
}

我不断收到错误:Uncaught TypeError: (0 , _firebase.auth) is not a function

我用谷歌搜索了这个错误,但没有看到任何结果。

我做错了什么?谢谢!

最佳答案

在 firebase.js 中,您已经在调用 firebase.auth() 并导出对象。您不应该再次调用导入的 auth 对象,只需访问导入的 auth 对象并调用其上的下一个函数即可。

例如。
而不是 auth().signInWithEmailAndPassword 尝试 auth.signInWithEmailAndPassword

关于javascript - 未捕获的 TypeError : (0 , _firebase.auth) 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48613086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com