简体   繁体   English

undefined 不是带有 fetch 的 object(评估“this.props.navigation”)

[英]undefined is not an object (evaluating 'this.props.navigation') with fetch

I try my best to make a Sign-in form with React native but:我尽力使用 React Native 制作登录表单,但是:

  • I can't make a redirection to 'App', the error message is: (TypeError: undefined is not an object (evaluating 'this.props.navigation')])我无法重定向到“App”,错误消息是:(TypeError: undefined is not an object (evalating 'this.props.navigation')])

     try { fetch('http://93.xxx.xx.xx:5151/login', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, body: formBody, }).then(function(response) { if (response.ok) { this.props.navigation.navigate('App'); } else { throw new Error("Failed to fetch [error: " + response.status + "]"); Alert.alert("Error [" + response.status + "] - " + response.statusText); } }).then(function(response) { if (response.ok) { Alert.alert(response.userToken); console.log(response); } else { Alert.alert("Error [" + response.status + "] - " + response.statusText); } }) } catch (error) { if (error) { console.error(error); } }

Is anyone know how to do that?有人知道该怎么做吗?

  • I only have one solution so far:到目前为止,我只有一个解决方案:

     fetch('http://93.xxx.xx.xx:5151/login', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, body: formBody }).then(res => { if (res.ok) { this.props.navigation.navigate('App') } else { if (res.status == 400) { Alert.alert("Error 400: login rejected because -> " + res.message) } else { throw Error(`Request rejected with status ${res.status}`); } } }).catch(console.error) }

But with this solution i don't know how to save the User token...但是有了这个解决方案,我不知道如何保存用户令牌......

Its a scoping issue, change this:这是一个范围界定问题,请更改:

.then(function(response) { // due to this you are losing `this` scope inside it

// due to that, this is not accessible, in result you will get error
this.props.navigation.navigate('App');  

To:至:

.then((response) => { // <--- Fat arrow is what you need

The first one is a scope issue.第一个是 scope 问题。 If you want to use "this" in an anonymous function, you need to bind it to your object.如果你想在匿名 function 中使用“this”,你需要将它绑定到你的 object。 There are two ways to do that:有两种方法可以做到这一点:

1) If you use the old function style, the former object don't get automatically bound to it. 1)如果您使用旧的 function 样式,以前的 object 不会自动绑定到它。 Therefore you need to bind the parent object manually.因此,您需要手动绑定父 object。 If you want a little more explanation about it, look here: What is the use of the JavaScript 'bind' method?如果您想对此进行更多解释,请看这里: JavaScript 'bind' 方法的用途是什么?

Promise.then(function(res) {
    return "Your return"
}.bind(this));

2) The second way is to use the ES6 "Fat arrow"-Function. 2)第二种方式是使用ES6的“胖箭头”-Function。 This works internally a bit different and binds the content of the parent Obejct directly.这在内部工作有点不同,并直接绑定父对象的内容。

Promise.then(res => "Your return");

On your second Problem I don't fully understand what your goal is.关于你的第二个问题,我不完全理解你的目标是什么。 Do you want the user token in your next route?你想在你的下一条路线中使用用户令牌吗? If so, you should use "setParams":如果是这样,您应该使用“setParams”:

fetch('http://93.xxx.xx.xx:5151/login', { 
 method: 'POST', 
 headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}, 
 body: formBody 
})
.then(res => {
 if(res.ok) {
  this.props.setParams({
    token: res.userToken
  })
  this.props.navigation.navigate('App')
 } else {
  if (res.status == 400) {
    Alert.alert("Error 400 : login rejected because -> " + res.message)
  } else {
    throw Error(`Request rejected with status ${res.status}`);
  }
}})
.catch(console.error)
}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义不是对象(评估“ this.props.navigation”) - undefined is not an object (evaluating 'this.props.navigation") 未定义不是对象(评估“ this.props.navigation”) - undefined is not an object (evaluating 'this.props.navigation') undefined不是对象(评估this.props.navigation) - undefined is not an object (Evaluating this.props.navigation) 未定义不是对象(评估“ this.props.navigation”) - undefined is not an object (evaluating “this.props.navigation”) undefined 不是一个对象(评估&#39;this.props.navigation&#39;)-React Native - undefined is not an object (evaluating 'this.props.navigation') - React Native TypeError:undefined 不是 React Native 中的对象(评估“_this.props.navigation”) - TypeError: undefined is not an object (evaluating '_this.props.navigation') in React Native 如何修复“未定义不是 object(评估 'this.props.navigation')” - How to fix “undefined is not an object (evaluating 'this.props.navigation')” undefined不是评估_this.props.navigation React Native的对象 - undefined is not an object evaluating _this.props.navigation React Native 未定义不是对象(评估&#39;_this.props.navigation&#39;) - Undifined is not an object(evaluating '_this.props.navigation') 如何解决React Native中的导航问题(TypeError:undefined不是一个对象(评估&#39;_this.props.navigation)) - How to fix navigation problem in React Native (TypeError: undefined is not an object (evaluating '_this.props.navigation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM