Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6212

fetch request works in componentDidMount but bot in seperate typescript file

$
0
0

I Have a React Native app configured with typescript.Tried to do a simple fetch:

  fetch('https://accounts.ea.com/connect/auth?client_id=FIFA-20-WEBCLIENT&response_type=token')    .then((res) => {        console.log('log');        console.log(res);        return res.json();    })    .then((data) => {        console.log(data);    })    .catch((err) => {        console.log('ERROR->'+ err);    });

when I did it in a seperate .ts file and debugged it with chrome debugger that has this configuaration:

{"type": "node","request": "launch","name": "Debug TS","program": "${file}","preLaunchTask": "tsc: build - tsconfig.json","outFiles": ["${workspaceFolder}/build/**/*.js"],"console": "integratedTerminal"}

the fetch didn't pass and I got this error:

ERROR->FetchError: maximum redirect reached at: https://signin.ea.com/p/web2/login?execution=e39798106s1&initref=https%3A%2F%2Faccounts.ea.com%3A443%2Fconnect%2Fauth%3Fclient_id%3DFIFA-20-WEBCLIENT%26response_type%3Dtoken

Then I tried to do the same in debug, and extracted the fetch into a function that was called from ComponentDidMount inside the component, it actually worked and responded with 200 status.

The debugger I used to debug the React Native from vscode is configured as following:

{"name": "Debug Attached Android","cwd": "${workspaceFolder}","type": "reactnative","request": "attach"  }

My typescript file attempt:test.ts-didn't work

import EAAuth from './ea-auth';import { AuthMethod } from './enums/auth-method';const eaAuth = new EAAuth('xxx@gmail.com', 'xxxxxx', 'xx', AuthMethod.SMS);eaAuth.firstLoginToWebApp();

tried it like this:

App.tsx-that works

class App extends React.Component {  componentDidMount() {    const eaAuth = new EAAuth('xxx@gmail.com', 'xxxxxx', 'xx', AuthMethod.SMS);    eaAuth.firstLoginToWebApp();  }  render() {    return <Text>asdasd</Text>;  }

}

Also tried to fetch http://google.co.il from a seperate .ts file and that worked as well to my surprise.What may cause this behavior, is it related to the request or to the typescript configuration? As I see it, it can't be the typescript - since google does work, and it can't be related to the debugger because of the same reason. So what am I missing out ?thanks in advance.


Viewing all articles
Browse latest Browse all 6212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>