React native axios crashing and werid behaviour

Viewed 94

Axios is behaving weird on my react native webview if you have any idea how to solve or how i can track the problem it would be of much help.

On the onMessage of the webview i recieve the html of the website so i can get a specific link. I send the message when the user taps anywhere on screen.

Injected js:

var body_of_html = document.body;
        
body_of_html.addEventListener('click',function(e){
          window.ReactNativeWebView.postMessage(document.documentElement.innerHTML)
},false);

Case 1

This doesnt console.log anything and after some time crashes the app.

      onMessage={event => {
          // console.log('asdas');
          var regex = new RegExp('token=[a-zA-z0-9]+');
          if (event.nativeEvent.data.toString().match(regex) != null) {
            let asd =
              'https://fvs.io/redirector?' +
              event.nativeEvent.data.toString().match(regex);
            axios.get(asd).then(rs => {
              console.log(rs);
            });

Case 2

This one works perfectly fine and logs "Anything".

  onMessage={event => {
      var regex = new RegExp('token=[a-zA-z0-9]+');
      if (event.nativeEvent.data.toString().match(regex) != null) {
        let asd =
          'https://fvs.io/redirector?' +
          event.nativeEvent.data.toString().match(regex);
        axios.get(asd).then(console.log("Anything"));

As you can see from the above cases i am unable to get the response from the axios call. Which always after some time crashes the app. Am i doing something wrong on reciving the response ?

Edit: I think I know what might have caused my application to crash but this is just what I found after looking at the data consumed. The link i was sending to the axios.get was retriving bits of a video until it fully buffered. But the way my code was , it would do this action each time i tapped the screen. I guess at some point axios couldnt handle reciving 10x + videos at 1080p at the same time. Just to clarify my intention was just to get the redirection link i didnt know it would cause the video to buffer.

1 Answers

As in all promises, in order to debug the error, e.g. in cases where event.nativeEvent.data may be undefined, causing .toString() to throw error, use a catch block.

   axios.get(asd).then(rs => {
              console.log(rs);
            }).catch(error => console.log("error from axios", error))
Related