React-native-code-push: undefined is not a function (evaluating 'fetch(~)')

Created on 15 Dec 2016  路  11Comments  路  Source: microsoft/react-native-code-push

"react-native-code-push": "^1.16.1-beta",
"react-native": "^0.39.2",

  • Does this reproduce on a simulator, or only on a physical device? both.
  • Repro steps and/or stack trace:
    Release:
    [CodePush] Checking for update.
    rendering LoginScene
    [CodePush] Reporting binary update (1.0)
    [CodePush] Report status failed: {"appVersion":"1.0"}
    [CodePush] An unknown error occurred.
    [CodePush] undefined is not a function (evaluating 'fetch(n,{method:o(e),headers:a,body:t})')
    Debug:
    same but:
    [CodePush] undefined is not a function (evaluating 'fetch(url,{method:getHttpMethodName(verb), headers:headers,body:requestBody})')

I installed code push using the manual way. I tried upgrading / downgrading react-native with no result.
Somebody knows the issue?

cheers

Most helpful comment

I get a solution.

The problem happens because the function that call fetch has been called inside a AsyncStorage promise. It must be some context problem.

So, I just changed my workflow and fetch works fine.

All 11 comments

@kuhbaar, fetch API is usually polyfilled by react-native (see InitializeCore.js#L171) at JS startup. Looks like in your case there is another code that overwrites global fetch function. Could you please check if fetch is available in your project without invoking CodePush first?

@vladimir-kotikov, thanks for the quick response.

I'm using fetch multiple times for some async calls to a REST API:
let res = await fetch(url + uriquery + "?format=" + format +"&limit=1");
and

let res = await fetch(url, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      "amount": amount,
      "payment_method_nonce": nonce,
    })
  })

I'm also importing (but not using yet) 'react-native-fetch-blob'. But i don't think that correlates in anyway.

I can see both calls are somewhat similar to the error. Can I use CodePush without overwriting those methods? Thanks for your help!

@vladimir-kotikov
I tried removing all the fetch calls and the react-native-fetch-blob package, but the error still persists.

Can you tell me more on how I can check if the global fetch function has been overwritten?

Thanks

I'm also importing (but not using yet) 'react-native-fetch-blob'. But i don't think that correlates in anyway

I actually was thinking that react-native-fetch-blob might be a possible reason of the issue, since one of it's usecases is overwriting global fetch to seamlessly use extended behavior - there are a couple of typical issues, caused by this replacement (see wkh237/react-native-fetch-blob#58 and wkh237/react-native-fetch-blob#190)

However if you say that even after removing react-native-fetch-blob the issue still exists the cause of the problem must be something different. I'm currently out of ideas what might cause the error and would appreciate if you could provide a minimal app where the problem reproduces

Another options that I can suggest - you could try to set some breakpoints in node_modules/react-native-code-push/request-fetch-adapter.js around lines 23 and 33 and see what is fetch/window.fetch in your case.

@vladimir-kotikov
I will try it when I get home.

But is there any possibility of using both packages?

Thanks

But is there any possibility of using both packages?

I think yes, because react-native-fetch-blob's fetch is available as RNFetchBlob.fetch (https://github.com/wkh237/react-native-fetch-blob#download-example--fetch-files-that-needs-authorization-token) and shouldn't conflict with original fetch unless there is some misconfiguration

@vladimir-kotikov
One thing I don't quite get: If you say my global fetch function is overwritten why does following code still work:

let res = await fetch(url, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      "amount": amount,
      "payment_method_nonce": nonce,
    })
  })

if the fetch function would have been overwritten, this wouldn't work?

@vladimir-kotikov
Very interesting behavior: enabling remote debugging magically repairs the problem?
As soon as I activate remote debugging, I get the alert "Update available" from code push...

in the release.apk the problem persists

After some restarts it seems to work consistently... Some serious xmas magic happening here.

Hi, I'm using react-native 0.41.2, react-native-cli 2.0.1 with Android 6.0 (simulator) , and I'm having the same problem with fetch.

Initially fetch works fine, but after press R+R to realod application, fetch dont works anymore with the message: undefined is not a function (evaluating 'fetch ...

Any suggestions?

I get a solution.

The problem happens because the function that call fetch has been called inside a AsyncStorage promise. It must be some context problem.

So, I just changed my workflow and fetch works fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fanzhiri picture fanzhiri  路  3Comments

diegocouto picture diegocouto  路  4Comments

vira-khdr picture vira-khdr  路  3Comments

osdnk picture osdnk  路  3Comments

quanzaiyu picture quanzaiyu  路  3Comments