Javascript 对象(…)不是函数(react&x2B;firebase)
下面是我将在Join.js中调用的auth函数Javascript 对象(…)不是函数(react&x2B;firebase),javascript,reactjs,firebase,firebase-authentication,Javascript,Reactjs,Firebase,Firebase Authentication,下面是我将在Join.js中调用的auth函数 import { firebaseAuth } from './firebase' export function auth (email, pw) { return firebaseAuth().createUserWithEmailAndPassword(email, pw) } 所以我的return语句出错了,它说对象(…)不是函数。因此,在我的Join.js中,我console.log和this.email.value没有定义
import { firebaseAuth } from './firebase'
export function auth (email, pw) {
return firebaseAuth().createUserWithEmailAndPassword(email, pw)
}
所以我的return语句出错了,它说对象(…)不是函数。因此,在我的Join.js中,我console.log和this.email.value没有定义
handleSubmit = (e) => {
e.preventDefault()
console.log(`${this.email.value}`);
auth(this.email.value, this.pw.value)
.catch(e => this.setState(setErrorMsg(e)))
}
以下是我从用户处接受的表格
<Form size='large' onSubmit={this.handleSubmit}>
<Form.Group>
<label >Username</label>
<Input className="Name" ref={(username) => this.username = username} />
</Form.Group>
<Form.Group>
<label>Password</label>
<Input type="password" className="PW" ref={(pw) => this.pw = pw} />
</Form.Group>
<Form.Group>
<label>Confirm Password</label>
<Input type="password" className="JoinConfirmPW" />
</Form.Group>
<Form.Group>
<label>Email Address</label>
<Input className="JoinEmail" ref={(email) => this.email = email}/>
</Form.Group>
<Form.Group>
<label>Gender</label>
<Form.Select className="JoinGender" options={options} placeholder='Gender' ref={(gender) => this.gender = gender} />
</Form.Group>
<Button color='teal' className="submit">Sign Up</Button>
</Form>
用户名
this.username=username}/>
密码
this.pw=pw}/>
确认密码
电子邮件地址
this.email=email}/>
性别
this.gender=gender}/>
注册
我不知道为什么我没有定义。有人能帮忙吗?看起来您的
firebaseAuth()
未定义
您的firebase.js文件应该如下所示:
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/database';
...
const db = firebase.database();
const firebaseAuth = firebase.auth();
export {
db,
firebaseAuth,
};
auth的用法如下:
firebaseAuth.doCreateUserWithEmailAndPassword(email, passwordOne)
.then(authUser => {
// Create a user in your own accessible Firebase Database too
db.doCreateUser(authUser.user.uid, username, email)
.then(() => {
this.setState(() => ({ ...INITIAL_STATE }));
history.push(routes.HOME);
})
.catch(error => {
this.setState(byPropKey('error', error));
});
})
.catch(error => {
this.setState(byPropKey('error', error));
});
代码中有两个错误。首先,不要将输入字段的值存储在局部变量中。相反,使用state并使用
this.setState()
方法设置值。我知道这听起来很傻,不应该有太大的区别,但它消除了任何范围相关问题的可能性
其次,您正在拨打的firebase电话无效。它应该类似于firebase.auth().createUserWithEmailAndPassword(this.state.email,this.state.password)代码>
假设您在状态变量中有值。否则,使用适当的变量作为参数来传递电子邮件和密码。希望它能工作,因为它对我有用。所以我通过从react firebase(Github)复制整个firebase.js来解决这个错误。我不知道我为什么会犯这个错误,但至少我能解决它。