首页 > 解决方案 > 只有 Android 用户在 createuserwithemailandpassword 上收到此错误

问题描述

在 iOS 上,这从来都不是问题,但我的很多用户都在尝试创建一个 firebase 用户,然后我将新创建的用户信息写入实时数据库。它的命中或错过,一些用户成功运行,有时需要多次尝试。让我补充一点,我只参与这个项目的时间很短,我已经可以看出没有使用最佳实践。以下是代码:

使用 crashlytics,我看到以下错误:致命异常:com.facebook.react.common.JavascriptException null 不是对象(正在评估 't.navigator.dispatch'),堆栈:@364:2006 value@49:1280 @ 605:1154 value@49:1280 @590:497 value@49:1280 value@28:3311 @28:822 value@28:2565 value@28:794 value@-1

屏幕/login.js

import React, { Component } from 'react';
import { ... } from 'react-native';
import { connect } from 'react-redux';
import { authActions, ... } from '../redux/actions';
import firebase from 'react-native-firebase';

class Login extends Component {
  static navigationOptions = () => ({
    headerMode: 'none',
    header: null,
  });

  constructor(props) {
    super(props);
    this.state = {
      email: '',
      password: '',
      forceCheckEmail: false,
      forceCheckPassword: false,
    };
  }

  componentWillReceiveProps(newProps) {
    const { props } = this;
    const {
      error,
      isBusy,
      dismissError,
      screenProps: {
        modal: {
          setContent,
          clearContent,
          setDismissHandler,
        },
      },
    } = newProps;

    if (props.error !== error || props.isBusy !== isBusy) {
      const modalContent =
        isBusy ? <Spinner text='One moment...' /> :
        error ? <ErrorPopup message={error} /> :
        null;

      if (modalContent) {
        setContent(modalContent, undefined, this.ref);
        setDismissHandler(() => {
          this.setState({ showForgotBlock: true })
          dismissError();
        });
      } else {
        clearContent();
      }
    }
  }

  handleLogin() {
    Keyboard.dismiss();

    this.props.login({
      email: this.state.email,
      password: this.state.password,
    });
  }

  render() {
    const {
      keyboardIsVisible,
      email,
      password,
      forceCheckEmail,
      forceCheckPassword,
      showForgotBlock,
    } = this.state;

    const {
...
      navigation: {
        navigate
      }
    } = this.props;

    const emailValid = validateEmail(email);
    const passwordValid = password.length > 5;
    const loginEnabled = email !== '' && emailValid && passwordValid;

    const forgotPasswordBlock = showForgotBlock ? (
      <TouchableOpacity
        onPress={() => restorePassword(email)}
        style={{marginTop: -20, marginBottom: 10}}
      >
        <Text style={{color: '#777'}}>
          Forgot your password?
        </Text>
      </TouchableOpacity>
    ): null;

    firebase.analytics().setCurrentScreen('login', 'login');

    return (

        ...

          <TextInput
            style={[styles.input, forceCheckEmail && !emailValid ? styles.failedInput : null]}
            autoCorrect={false}
            placeholder="Email"
            onBlur={() => this.setState({ forceCheckEmail: true })}
            autoCapitalize="none"
            keyboardType="email-address"
            placeholderTextColor={color.INPUT_TEXT}
            onChangeText={email => this.setState({ email })}
            value={email}
          />
          <TextInput
            style={[styles.input, forceCheckPassword && !passwordValid ? styles.failedInput : null]}
            autoCorrect={false}
            placeholder="Password"
            onBlur={() => this.setState({ forceCheckPassword: true })}
            placeholderTextColor={color.INPUT_TEXT}
            secureTextEntry
            onChangeText={password => this.setState({ password })}
            value={password}
          />

          ...

          <TouchableOpacity
            style={[styles.button, styles.buttonPrimary]}
            onPress={() => navigate('SignUp')}
          >
            <Text style={styles.buttonPrimaryText}>
              SIGN UP
            </Text>
          </TouchableOpacity>

...

export default connect(
  state => ({
    ...
  }),
  {
    login: data => authActions.login(data),

    ...
  },
)(Login);

动作/auth.js

import { createActions } from 'redux-feline-actions';// I question this dependency
import firebase from 'react-native-firebase';
import FBSDK from 'react-native-fbsdk';

const usersDB = firebase.database().ref('users');
const newUserData = {
  point: 0,
  savedNumbers: [],
};

export default createActions({
...
 register: ({ name, email, phone, password }) => ({
         useReducer: 'auth',
         payload: firebase.auth()
           .createUserWithEmailAndPassword(email, password)
           .then(({user: { uid, email }}) => usersDB
         .child(uid)
         .set({
           ...newUserData,
           name,
           email,
           phone,
           id: uid,
         })
         .then(err => err || ({
           ...newUserData,
           name,
           email,
           phone,
           id: uid,
         }))),
  }),
...

商店/auth.js

import Immutable, { Map } from 'immutable';
import createAsyncStores from 'cat-stores'; // I also question this one

export default createAsyncStores({
  auth: {
    begin: state => state
      .set('isBusy', true),
    complete: (state, { payload }) => state
      .set('isBusy', false)
      .set('user', Immutable.fromJS(payload)),
    error: {
      default: (state, { payload }) => state
        .set('error', payload.message)
        .set('isBusy', false)
        .set('user', null), // Android users keep getting this result I believe
    },
  },
...
},
Map({
  isBusy: false,
  error: null,
  user: null,
  redirectTo: null,
  theme: Map(),
  settings: Map(),
  themeIsLoaded: false,
  settingsAreLoaded: false,
}));

我希望用户在 Android 上创建和保存新用户信息时不会遇到问题,就像在 iOS 上一样。

标签: react-nativereact-reduxreact-native-firebase

解决方案


推荐阅读