我有一个名为FirebaseSvc
的firebase服务类,我在名为Register
的组件中使用它。我想要做的是运行reducer函数作为firebase createUserWithEmailAndPassword
函数的回调。
FirebaseSvc.js用户创建功能:
createAccount = async (user, success_callback, failed_callback) => {
await firebase.auth()
.createUserWithEmailAndPassword(user.email, user.password);
.then(success_callback, failed_callback);
}
Register.js提交用户注册表:
handleSubmit = () => {
const value = this._form.getValue();
axios.post(Api.API_URL + 'user', {
'user': this._form.getValue()
})
.then((response) => {
const user_credentials = { email: response.data.user.email, password: value.password };
if (response.status === 200) {
firebaseSvc.fetchingSignInMethodsForEmail(
user_credentials,
function(){
console.log('register success');
this.props.navigation.state.params.addUser(response.data.user);
this.props.navigation.state.params.addCoupledUsers(response.data.extra_data.coupled_users);
console.log('trying to navigate to homescreen');
this.props.navigation.navigate('HomeScreen');
},
function(){
console.log('register failed');
}
);
}
})
.catch((error) => {
console.log(error.config);
});
}
用户成功注册后,我在控制台日志register success
中收到此消息,然后它给出了一个错误说:
可能未处理的承诺拒绝(id:0)
TypeError:undefined不是对象(评估'this.props.navigation`)
我看到了一些可以解决错误的方法:
this
正确绑定到组件实例。替换此功能:
function() {
console.log('register success');
this.props.navigation.state.params.addUser(response.data.user); this.props.navigation.state.params.addCoupledUsers(response.data.extra_data.coupled_users);
console.log('trying to navigate to homescreen');
this.props.navigation.navigate('HomeScreen');
}
同
() => {
console.log('register success');
this.props.navigation.state.params.addUser(response.data.user);
this.props.navigation.state.params.addCoupledUsers(response.data.extra_data.coupled_users);
console.log('trying to navigate to homescreen');
this.props.navigation.navigate('HomeScreen');
}
firebaseSvc.fetchingSignInMethodsForEmail
中的错误目前没有达到.catch
方法,因此你看到Possible Unhandled Promise Rejection (id: 0)
。要解决此问题,请在return
之前添加firebaseSvc.fetchingSignInMethodsForEmail
return firebaseSvc.fetchingSignInMethodsForEmail(
user_credentials,
...