ReferenceError: fetch is not defined after upgrading
See original GitHub issueI asked this already at Stackoverflow but will do it here again for the sake of visibility.
I recently upgraded redux-api-middleware
from 2.3.0 to 3.0.1 When running my tests with jest@22.2.1
I get this ReferenceError
:
ReferenceError: fetch is not defined
19 | export function sortPolicyChain (payload: Array<ChainPolicy>): SortPolicyChainAction {
20 | return { type: 'SORT_POLICY_CHAIN', payload }
> 21 | }
22 |
23 | export type UpdatePolicyInChainAction = { type: 'UPDATE_POLICY_IN_CHAIN', policyConfig: ChainPolicy }
24 | export function updatePolicyInChain (policyConfig: ChainPolicy): UpdatePolicyInChainAction {
at Object.<anonymous> (node_modules/redux-api-middleware/lib/index.cjs.js:404:3)
at Object.<anonymous> (app/javascript/src/Policies/actions/PolicyChain.jsx:21:27)
at Object.<anonymous> (spec/javascripts/Policies/actions/PolicyChain.spec.jsx:3:20)
The stacktrace points to this line in PolicyChain.jsx:
export function fetchChain (serviceId: string): RSSAAction {
return {
[RSAA]: {
endpoint: `/admin/api/services/${serviceId}/proxy/policies.json`,
method: 'GET',
credentials: 'same-origin',
types: [REQUEST, SUCCESS, FAILURE]
}
}
}
According to the documentation here:
If provided, the fetch option must be a function that conforms to the Fetch API. Otherwise, the global fetch will be used.
But it looks like the global fetch can’t be found. Any ideas?
Relevant code at https://github.com/3scale/porta/pull/463.
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (1 by maintainers)
Top Results From Across the Web
ReferenceError: fetch is not defined - javascript - Stack Overflow
If you use the new version(3.0.0) , it will get an error in import and then you'll get another one that says "fetch...
Read more >How to fix 'ReferenceError: fetch is not defined' in Node.js
First, let's replicate the issue. You can update the index.js to the following and run node index.js , you should be able to...
Read more >ReferenceError: fetch is not defined in NodeJs | bobbyhadz
The "ReferenceError: fetch is not defined" occurs when the fetch() method is used in an environment where it's not supported - most commonly...
Read more >fetch is not defined · Issue #1685 · TypeStrong/ts-node - GitHub
It throws this error: yarn run v1. 22.17 $ ts-node ./fetch-test. ts ReferenceError: fetch is not defined at /Users/zomars/Sites/test/fetch-test.
Read more >Fetch is not defined error fixed nodejs ... - YouTube
In this video ,I try to fix an error ReferenceError : fetch is not defined in node js .Fetch can not use to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is also a problem for isomorphic apps. What makes it especially problematic is that the code where the global fetch is assumed happens in the global scope of middleware.js, so an error is thrown as soon redux-api-middleware is included (ie, on startup). So we don’t even get a chance to pass in fetch from
node-fetch
or the like, as we were in 2.3.0, and our app fails out of the gate.I propose waiting until within
createMiddleware()
to set the default value offetch
, assuming it wasn’t passed in throughoptions
.Proper sollution I think is to drop apiMiddleware in favor of createMiddleware. And get rid of all global dependencies.
I’ll take a look again when will have some time.