Skip to content

undefined is not a function (evaluating 'fetch(~)') #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kuhbaar opened this issue Dec 15, 2016 · 11 comments
Closed

undefined is not a function (evaluating 'fetch(~)') #644

kuhbaar opened this issue Dec 15, 2016 · 11 comments

Comments

@kuhbaar
Copy link

kuhbaar commented Dec 15, 2016

"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

@vladimir-kotikov
Copy link

@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?

@kuhbaar
Copy link
Author

kuhbaar commented Dec 16, 2016

@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!

@kuhbaar
Copy link
Author

kuhbaar commented Dec 17, 2016

@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

@vladimir-kotikov
Copy link

vladimir-kotikov commented Dec 19, 2016

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.

@kuhbaar
Copy link
Author

kuhbaar commented Dec 19, 2016

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

But is there any possibility of using both packages?

Thanks

@vladimir-kotikov
Copy link

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

@kuhbaar
Copy link
Author

kuhbaar commented Dec 19, 2016

@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?

@kuhbaar
Copy link
Author

kuhbaar commented Dec 19, 2016

@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

@kuhbaar
Copy link
Author

kuhbaar commented Dec 19, 2016

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

@kuhbaar kuhbaar closed this as completed Dec 19, 2016
@douglasjunior
Copy link

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?

@douglasjunior
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants