TypeError:未定义不是对象(评估“ personalDetails.firstName”)

时间:2019-08-28 20:07:33

标签: android react-native

我遇到的问题是,我可以在ios端登录我的React Native应用程序,但不能在android端登录。

我在logcat中遇到的错误说:

2019-08-28 14:33:01.638 31244-17613/com.nfibengage E/ReactNativeJS: TypeError: undefined is not an object (evaluating 'personalDetails.firstName')

    This error is located at:
        in DetailsConfirmation (created by Connect(DetailsConfirmation))
        in Connect(DetailsConfirmation) (at SceneView.js:17)
        in SceneView (at CardStack.js:455)
        in RCTView (at View.js:35)
        in View (at CardStack.js:454)
        in RCTView (at View.js:35)
        in View (at CardStack.js:453)
        in RCTView (at View.js:35)
        in View (at createAnimatedComponent.js:151)
        in AnimatedComponent (at Card.js:12)
        in Card (at PointerEventsContainer.js:39)
        in Container (at CardStack.js:498)
        in RCTView (at View.js:35)
        in View (at CardStack.js:414)
        in RCTView (at View.js:35)
        in View (at CardStack.js:413)
        in CardStack (at CardStackTransitioner.js:67)
        in RCTView (at View.js:35)
        in View (at Transitioner.js:142)
        in Transitioner (at CardStackTransitioner.js:19)
        in CardStackTransitioner (at StackNavigator.js:41)
        in Unknown (at createNavigator.js:13)
        in Navigator (at createNavigationContainer.js:226)
        in NavigationContainer (at RootNavigation.js:216)
        in RootNavigation (created by Connect(RootNavigation))
        in Connect(RootNavigation) (at App.js:249)
        in RCTView (at View.js:35)
        in View (at App.js:247)
        in Provider (at App.js:246)
        in NFIBEngage (at renderApplication.js:40)
        in RCTView (at View.js:35)
        in View (at AppContainer.js:98)
        in RCTView (at View.js:35)
        in View (at AppContainer.js:115)
        in AppContainer (at renderApplication.js:39)
2019-08-28 14:33:29.127 1738-1800/? E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0
2019-08-28 14:33:42.670 1822-1822/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2019-08-28 14:33:42.671 1822-1822/? E/netmgr: WifiForwarder unable to open QEMU pipe: Invalid argument

我注意到DetailsConfirmation.js内部:

 constructor(props) {
    super(props);
    const { personalDetails, personalAddress } = this.props.registeredUser;
    console.log(this.props.registeredUser);
    if (personalDetails) {
      this.props.firstNameChanged(personalDetails.firstName);
      this.props.lastNameChanged(personalDetails.lastName);
      this.props.suffixChanged(personalDetails.suffix);
      this.props.prefixChanged(personalDetails.prefix);
    }

    if (personalAddress && personalAddress.stateCode) {
      this.props.stateChanged(personalAddress.stateCode);
    }

    const { params = {} } = this.props.navigation.state;
    const { displayAlert = true } = params;

    this.state = {
      validationErrors: {},
      displayErrors: false,
      titleName: personalDetails.firstName,
      displayAlert
    };
  }

  componentWillReceiveProps(nextProps) {
    if (this.state.displayErrors) {
      this._validate(nextProps);
    }
  }

它们全部都在构造函数中,实际上应该放在componentDidMount()中。我尝试了这种方法,但是并没有为我解决错误。

1 个答案:

答案 0 :(得分:0)

this.state = {
  validationErrors: {},
  displayErrors: false,
  titleName: personalDetails.firstName, // <--- it causes an error
  displayAlert
};

您可以避免这样的错误:

this.state = {
  validationErrors: {},
  displayErrors: false,
  titleName: personalDetails && personalDetails.firstName, // <--- it should be ok
  displayAlert
};