反应原生是否支持.error()函数?(Does react native supports .error() function?)
我试图处理“网络请求失败”错误(我已经允许来自Xcode的不安全连接),但是当我尝试在.error()函数中做某事时,它崩溃并说:.error不是函数; fetch()已经可以工作了。 这是一个例子,我试图在获取功能后运行此代码。
fetch('URL',{ 'method': 'POST', 'headers': { 'Accept': 'application/json', 'Content-Type': 'application/json', }, 'body': data} ) .then((response) => response.json()) .then((responseJson) => { //Do something }) .error((error)=>{ console.error(error); Actions.login() });Im trying to handle a "Network request failed" error (I already allowed unsecured connections from Xcode), But when i try to do something inside the .error() function it crashes and says: .error is not a function; The fetch() works already. here's an example of it, Im trying to run this code after a fetch function.
fetch('URL',{ 'method': 'POST', 'headers': { 'Accept': 'application/json', 'Content-Type': 'application/json', }, 'body': data} ) .then((response) => response.json()) .then((responseJson) => { //Do something }) .error((error)=>{ console.error(error); Actions.login() });最满意答案
React的fetch方法返回一个Promise ,它没有.error()方法。 相反,它具有.catch()方法来捕获fetch失败时的所有错误。
fetch('URL', { 'method': 'POST', 'headers': { 'Accept': 'application/json', 'Content-Type': 'application/json', }, 'body': data }) .then((response) => response.json()) .then((responseJson) => { //Do something }) .catch((error) => { console.error(error); Actions.login() });React's fetch method returns a Promise, which doesnot have .error() method. Instead it has .catch() method to catch all errors when fetch fails.
fetch('URL', { 'method': 'POST', 'headers': { 'Accept': 'application/json', 'Content-Type': 'application/json', }, 'body': data }) .then((response) => response.json()) .then((responseJson) => { //Do something }) .catch((error) => { console.error(error); Actions.login() });
更多推荐
发布评论