Javascript 未捕获类型错误:(0,_firebase.auth)不是函数
正在尝试在React应用程序中使用Firebase身份验证。 我遵循了firbase文档中的说明 安装了带npm的firebase 添加了firebase.js文件:Javascript 未捕获类型错误:(0,_firebase.auth)不是函数,javascript,reactjs,firebase,firebase-authentication,Javascript,Reactjs,Firebase,Firebase Authentication,正在尝试在React应用程序中使用Firebase身份验证。 我遵循了firbase文档中的说明 安装了带npm的firebase 添加了firebase.js文件: import * as firebase from 'firebase' var config = { apiKey: "AIzaSyBUORmX2qG7BXFC4hqfKAq0Y6HMAsLTGbw", authDomain: "hardy-brooklyn.firebaseapp.com", databaseUR
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,
};
调用了我的组件中的一个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>
);
}
}
“严格使用”;
从“React”导入React,{Component};
从“./../firebase”导入{auth}
导出默认类Nav扩展组件{
建造师(道具){
超级(道具);
此.state={
导航:“希德纳夫”,
登录:“hideLogin”,
电邮:“,
密码:“
};
this.handleChange=this.handleChange.bind(this);
this.handleSubmit=this.handleSubmit.bind(this);
}
手变(活动){
this.setState({email:event.target.value});
}
handleSubmit(事件){
event.preventDefault();
让email=this.state.email;
让password=this.state.password;
auth().signInWithEmailAndPassword(电子邮件,密码)。catch(函数(错误){
//在这里处理错误。
var errorCode=error.code;
var errorMessage=error.message;
});
auth().onAuthStateChanged(函数(用户){
如果(用户){
//用户已登录。
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(显示名称)
}否则{
console.log('无用户')
}
});
}
render(){
返回(
管理员只使用
电邮:
密码:
提交
隐藏
);
}
}
我一直收到错误:uncaughttypeerror:(0,_firebase.auth)不是函数
我用谷歌搜索了这个错误,没有看到任何结果
我做错了什么?谢谢 在firebase.js中,您已经在调用
firebase.auth()
并导出对象。您不应该再次调用导入的auth
对象,只需访问导入的auth对象并对其调用下一个函数即可
例如不要使用电子邮件和密码进行
auth()。如果问题解决了您的问题,请将其标记为已回答