TypeError:无法读取native native中未定义的属性'receiptnumber'

时间:2017-08-27 06:01:07

标签: javascript android ios reactjs react-native

我在组件的状态下有这些属性

constructor(props) {
  super(props);
  this._onPress = this._onPress.bind(this);
  this.state = {
    receiptnumber: "",
    text: "",
    time: "",
    isDateTimePickerVisible: false
  };
}

我已经对正在执行的按钮的点击事件绑定了_onPress方法。我在_saveScheduleMessage方法中调用了另一种方法_onPress,我试图访问this.state.receiptnumber我收到错误。

_onPress() {
  try {
      if (Platform.OS === "android") {
        if (Platform.Version >= 23) {
          Promise.resolve(requestSendSMSPermission()).then(function(result) {
            // user granted SMS permission
            if (result) {
              this._saveScheduleMessage;
            } else {
              // user denied SMS permission
              Alert.alert(
                strings.permission_required,
                strings.send_sms_deny_permission
              );
            }
          });
        }
      }
  } catch (error) {
    console.log(error);
  }
}

_saveScheduleMessage() {
  realm.write(() => {
    realm.create("NewMessage", {
      receiptNumber: this.state.receiptnumber,
      text: this.state.text,
      time: this.state.time
    });
  });
  const resetAction = NavigationActions.reset({
    index: 0,
    actions: [
      NavigationActions.navigate({
        routeName: "DashboardScreen"
      })
    ]
  });
  navigate.dispatch(resetAction);
}

PermissionManager.js

import { PermissionsAndroid } from "react-native";

module.exports = {
  requestSendSMSPermission: async function() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.SEND_SMS
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        return true;
      } else {
        return false;
      }
    } catch (err) {
      console.warn(err);
    }
  }
};

这就是我调用_onPress方法

的方法
<RoundButton
    textStyle={styles.roundTextStyle}
    buttonStyle={styles.roundButtonStyle}
    onPress={() => this._onPress()}
  >

错误日志:

D:\React Native\application\node_modules\react-native\Libraries\ReactNative\YellowBox.js:76 Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'receiptnumber' of undefined
TypeError: Cannot read property 'receiptnumber' of undefined
    at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:106446:60
    at tryCallOne (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:16011:12)
    at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:16097:15
    at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7370:19
    at _callTimer (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7284:7)
    at Object.callImmediatesPass (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7512:9)
    at Object.callImmediates (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7523:21)
    at MessageQueue.__callImmediates (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:6882:16)
    at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:6768:16
    at MessageQueue.__guard (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:6868:9)
console.warn @ D:\React Native\application\node_modules\react-native\Libraries\ReactNative\YellowBox.js:76
onUnhandled @ D:\React Native\application\node_modules\react-native\Libraries\Promise.js:35
onUnhandled @ D:\React Native\application\node_modules\promise\setimmediate\rejection-tracking.js:71
(anonymous) @ D:\React Native\application\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:223
_callTimer @ D:\React Native\application\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:143
callTimers @ D:\React Native\application\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:372
__callFunction @ D:\React Native\application\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:266
(anonymous) @ D:\React Native\application\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:103
__guard @ D:\React Native\application\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:231
callFunctionReturnFlushedQueue @ D:\React Native\application\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:102
(anonymous) @ debuggerWorker.js:72

有谁知道我怎么修理它?

当我删除Promise.resolve时它会起作用,但我需要它来获取权限。

_onPress() {
  realm.write(() => {
    realm.create("NewMessage", {
      receiptNumber: this.state.receiptnumber,
      text: this.state.text,
      time: this.state.time
    });
  });

  const resetAction = NavigationActions.reset({
    index: 0,
    actions: [
      NavigationActions.navigate({
        routeName: "DashboardScreen"
      })
    ]
  });
  this.props.navigation.dispatch(resetAction);
}  

1 个答案:

答案 0 :(得分:3)

尝试替换

function(result) 

result => 

我无法解释这个理论,但基本上这个'=&gt;'语法是让你的函数不能立即执行,

例如,如果你做这样的事情

onPress = {this.onPress}

即使没有按下事件,this.onPress方法也会立即执行

也许有些专家可以更恰当地解释这个问题