javascript - TypeError: undefined is not an object (evaluating 'this.props') - React Native - Stack Overflow

Im getting this type error when im trying to navigate to next view after getting json response.but my t

Im getting this type error when im trying to navigate to next view after getting json response.but my this.props.navigator.push({id:'HomeCaseList')} works well if i user out side of my json response getting method.can any one help me here.

click event

<MKButton
            style={{marginLeft:10, marginRight:10,marginTop:10,marginBottom:350,height:40, width:400}}
            backgroundColor={'#fff'}
            //shadowRadius={2}
            //shadowOffset={{width:0, height:2}}
            shadowOpacity={.7}
            shadowColor="black"
              onPress={
              //  navigator.push();

                this.loginToApp.bind(this)
              }>

login function

gotoNext() {
      fetch('http://url/login/', {
        credentials: 'same-origin',
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          username: 'wasalaa',
          password: 'a',
        })
      }).then(function(response) {
      //console.log(response.headers.get('Content-Type'))
      //console.log(response.headers.get('Date'))
      //console.log(response.status)
      //console.log(response.statusText)
      //console.log('json',response
      //console.log(response.headers.get('JSESSIONID'))
      return response.json()
      }).then(function(json){
      console.log('login response',json)
      if(json.message_code === "SUCCESS"){
        console.log('user logged')
        this.props.navigator.push({
          id: 'HomeCaseList',
          //sceneConfig: Navigator.SceneConfigs.FloatFromBottom,
        });
      }

      }).catch(function(err){
      console.log('error', err)
      })
  }

Im getting this type error when im trying to navigate to next view after getting json response.but my this.props.navigator.push({id:'HomeCaseList')} works well if i user out side of my json response getting method.can any one help me here.

click event

<MKButton
            style={{marginLeft:10, marginRight:10,marginTop:10,marginBottom:350,height:40, width:400}}
            backgroundColor={'#fff'}
            //shadowRadius={2}
            //shadowOffset={{width:0, height:2}}
            shadowOpacity={.7}
            shadowColor="black"
              onPress={
              //  navigator.push();

                this.loginToApp.bind(this)
              }>

login function

gotoNext() {
      fetch('http://url/login/', {
        credentials: 'same-origin',
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          username: 'wasalaa',
          password: 'a',
        })
      }).then(function(response) {
      //console.log(response.headers.get('Content-Type'))
      //console.log(response.headers.get('Date'))
      //console.log(response.status)
      //console.log(response.statusText)
      //console.log('json',response
      //console.log(response.headers.get('JSESSIONID'))
      return response.json()
      }).then(function(json){
      console.log('login response',json)
      if(json.message_code === "SUCCESS"){
        console.log('user logged')
        this.props.navigator.push({
          id: 'HomeCaseList',
          //sceneConfig: Navigator.SceneConfigs.FloatFromBottom,
        });
      }

      }).catch(function(err){
      console.log('error', err)
      })
  }
Share Improve this question asked Apr 9, 2016 at 14:52 hashhash 5,4167 gold badges41 silver badges59 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

In callback this refers to global scope(in browser it is window in node.js it is global, and if you use strict mode this will be undefined) and not for your ponent, you should set this

//....
.then(function(json) {
  if (json.message_code === "SUCCESS") {
    this.props.navigator.push({
      id: 'HomeCaseList',
      // sceneConfig: Navigator.SceneConfigs.FloatFromBottom
    });
  }
}.bind(this))
 ^^^^^
// ....

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745575539a4633939.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信