Spring React Chrome CORS error when accessed outside router - reactjs

I use Chrome as my default browser. I have cross origins configured in my Spring backend and React front end.
Spring: #CrossOrigin(origins = "http://localhost:3000")
React: const USER_API_BASE_URL = "http://localhost:8080/api/v1/users";
All works great when I am working on my PC. I configured NOIP and configured my router to route incoming to my development PC. When I start a session using the external address I get the CORS error:
...
Access to XMLHttpRequest at 'http://localhost:8080/api/v1/users' from origin 'http://TESTURL.ddns.net:3000' has been blocked by CORS policy: The request client is not a secure context and the resource is in more-private address space `local`.
xhr.js:187 GET http://localhost:8080/api/v1/users net::ERR_FAILED
dispatchXhrRequest # xhr.js:187
xhrAdapter # xhr.js:13
dispatchRequest # dispatchRequest.js:53
request # Axios.js:108
Axios.<computed> # Axios.js:129
wrap # bind.js:9
getUsers # UserService.js:8
componentDidMount # LoginComponent.jsx:19
commitLifeCycles # react-dom.development.js:19816
commitLayoutEffects # react-dom.development.js:22805
callCallback # react-dom.development.js:188
invokeGuardedCallbackDev # react-dom.development.js:237
invokeGuardedCallback # react-dom.development.js:292
commitRootImpl # react-dom.development.js:22543
unstable_runWithPriority # scheduler.development.js:653
runWithPriority$1 # react-dom.development.js:11039
commitRoot # react-dom.development.js:22383
finishSyncRender # react-dom.development.js:21809
performSyncWorkOnRoot # react-dom.development.js:21795
scheduleUpdateOnFiber # react-dom.development.js:21190
updateContainer # react-dom.development.js:24375
(anonymous) # react-dom.development.js:24760
unbatchedUpdates # react-dom.development.js:21905
legacyRenderSubtreeIntoContainer # react-dom.development.js:24759
render # react-dom.development.js:24842
(anonymous) # index.js:8
./src/index.js # index.js:19
__webpack_require__ # bootstrap:856
fn # bootstrap:150
1 # reportWebVitals.js:14
__webpack_require__ # bootstrap:856
checkDeferredModules # bootstrap:45
webpackJsonpCallback # bootstrap:32
(anonymous) # main.chunk.js:1
Show 4 more frames
createError.js:16 Uncaught (in promise) Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:99)
...
Is there a way around this?

The solution was
In the React front end changing
const USER_API_BASE_URL = "http://localhost:8080/api/v1/documenttype";
to
const USER_API_BASE_URL = "http://XXsystem.ddns.net:8080/api/v1/documenttype";
In the Spring backend changing
#CrossOrigin("http://localhost:3000")
to
#CrossOrigin(origins = "http://XXsystem.ddns.net:3000")
Interesting that the 'localhost testing still work !
Best of both worlds.

Related

How to create micro frontends based on different technologies using a common shell with module federation and NX?

I'm trying to create micro frontends with React and Angular (the remotes) that are used within a shell application based on React (the host/shell) using Webpacks Module Federation.
Therefore I used this official documentation provided by NX --> Advanced Angular Micro Frontends with Dynamic Module Federation
Here is what I did:
1. npx create-nx-workspace pace-microfrontends
2. npm install #nrwl/react --save-dev
3. npm install #nrwl/angular --save-dev
4. npx nx g #nrwl/react:host shell
5. npx nx g #nrwl/angular:remote angular-microfrontend --host=shell
6. npx nx g #nrwl/react:remote react-microfrontend --host=shell
So I created a fresh NX workspace, added the missing dependencies, generated the shell/host application based on React and added two micro frontends (one based on React, one based on Angular) that should be hosted within the shell.
After that it was needed to add the missing routing to the Angular micro frontend within the shell, because it was not generated by NX like for the React micro frontend, like this:
apps/shell/src/app/app.tsx
import * as React from 'react';
import NxWelcome from './nx-welcome';
import { Link, Route, Routes } from 'react-router-dom';
const ReactMicrofrontend = React.lazy(
() => import('react-microfrontend/Module')
);
const AngularMicrofrontend = React.lazy(
() => import('angular-microfrontend/Module')
);
export function App() {
return (
<React.Suspense fallback={null}>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/react-microfrontend">ReactMicrofrontend</Link>
</li>
<li>
<Link to="/angular-microfrontend">AngularMicrofrontend</Link>
</li>
</ul>
<Routes>
<Route path="/" element={<NxWelcome title="shell" />} />
<Route path="/react-microfrontend" element={<ReactMicrofrontend />} />
<Route
path="/angular-microfrontend"
element={<AngularMicrofrontend />}
/>
</Routes>
</React.Suspense>
);
}
export default App;
Since the module federation configuration was not right as well (it couldn't find the remoteEntry.js file), I also had to change it from this:
apps/shell/module-federation.config.js
// #ts-check
/**
* #type {import('#nrwl/devkit').ModuleFederationConfig}
**/
const moduleFederationConfig = {
name: 'shell',
remotes: [react-microfrontend'],
};
module.exports = moduleFederationConfig;
..to this:
apps/shell/module-federation.config.js
// #ts-check
/**
* #type {import('#nrwl/devkit').ModuleFederationConfig}
**/
const moduleFederationConfig = {
name: 'shell',
remotes: [
['angular-microfrontend', 'http://localhost:4201/remoteEntry.mjs'],
['react-microfrontend', 'http://localhost:4202/remoteEntry.js'],
],
};
module.exports = moduleFederationConfig;
After these adjustments it was possible to start the shell application with npm start.
The routing for Home and ReactMicrofrontend works fine but as soon as I click on AngularMicrofrontend I get the following errors:
react_devtools_backend.js:4012 Warning: lazy: Expected the result of a
dynamic import() call. Instead received: [object Module]
Your code should look like: const MyComponent = lazy(() =>
import('./MyComponent'))
at RenderedRoute (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:4840:5)
at Routes (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5272:5)
at Suspense
at App
at Router (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5204:15)
at BrowserRouter (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:3464:5)
overrideMethod # react_devtools_backend.js:4012 printWarning #
react.development.js:209 error # react.development.js:183
lazyInitializer # react.development.js:1400 resolveLazy #
react-dom.development.js:14907 reconcileSingleElement #
react-dom.development.js:15718 reconcileChildFibers #
react-dom.development.js:15808 reconcileChildren #
react-dom.development.js:19174 updateContextProvider #
react-dom.development.js:21154 beginWork #
react-dom.development.js:21649 beginWork$1 #
react-dom.development.js:27426 performUnitOfWork #
react-dom.development.js:26557 workLoopConcurrent #
react-dom.development.js:26543 renderRootConcurrent #
react-dom.development.js:26505 performConcurrentWorkOnRoot #
react-dom.development.js:25738 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react_devtools_backend.js:4012
Warning: lazy: Expected the result of a dynamic import() call. Instead
received: [object Module]
Your code should look like: const MyComponent = lazy(() =>
import('./MyComponent'))
at Lazy
at RenderedRoute (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:4840:5)
at Routes (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5272:5)
at Suspense
at App
at Router (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5204:15)
at BrowserRouter (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:3464:5)
overrideMethod # react_devtools_backend.js:4012 printWarning #
react.development.js:209 error # react.development.js:183
lazyInitializer # react.development.js:1400 mountLazyComponent #
react-dom.development.js:19944 beginWork #
react-dom.development.js:21593 beginWork$1 #
react-dom.development.js:27426 performUnitOfWork #
react-dom.development.js:26557 workLoopConcurrent #
react-dom.development.js:26543 renderRootConcurrent #
react-dom.development.js:26505 performConcurrentWorkOnRoot #
react-dom.development.js:25738 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react_devtools_backend.js:4012
Warning: lazy: Expected the result of a dynamic import() call. Instead
received: [object Module]
Your code should look like: const MyComponent = lazy(() =>
import('./MyComponent'))
at Lazy
at RenderedRoute (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:4840:5)
at Routes (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5272:5)
at Suspense
at App
at Router (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5204:15)
at BrowserRouter (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:3464:5)
overrideMethod # react_devtools_backend.js:4012 printWarning #
react.development.js:209 error # react.development.js:183
lazyInitializer # react.development.js:1400 mountLazyComponent #
react-dom.development.js:19944 beginWork #
react-dom.development.js:21593 callCallback #
react-dom.development.js:4164 invokeGuardedCallbackDev #
react-dom.development.js:4213 invokeGuardedCallback #
react-dom.development.js:4277 beginWork$1 #
react-dom.development.js:27451 performUnitOfWork #
react-dom.development.js:26557 workLoopConcurrent #
react-dom.development.js:26543 renderRootConcurrent #
react-dom.development.js:26505 performConcurrentWorkOnRoot #
react-dom.development.js:25738 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react-dom.development.js:20013
Uncaught Error: Element type is invalid. Received a promise that
resolves to: undefined. Lazy element type must resolve to a class or
function.
at mountLazyComponent (react-dom.development.js:20013:1)
at beginWork (react-dom.development.js:21593:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
at invokeGuardedCallback (react-dom.development.js:4277:1)
at beginWork$1 (react-dom.development.js:27451:1)
at performUnitOfWork (react-dom.development.js:26557:1)
at workLoopConcurrent (react-dom.development.js:26543:1)
at renderRootConcurrent (react-dom.development.js:26505:1)
at performConcurrentWorkOnRoot (react-dom.development.js:25738:1) mountLazyComponent # react-dom.development.js:20013 beginWork #
react-dom.development.js:21593 callCallback #
react-dom.development.js:4164 invokeGuardedCallbackDev #
react-dom.development.js:4213 invokeGuardedCallback #
react-dom.development.js:4277 beginWork$1 #
react-dom.development.js:27451 performUnitOfWork #
react-dom.development.js:26557 workLoopConcurrent #
react-dom.development.js:26543 renderRootConcurrent #
react-dom.development.js:26505 performConcurrentWorkOnRoot #
react-dom.development.js:25738 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react_devtools_backend.js:4012
Warning: lazy: Expected the result of a dynamic import() call. Instead
received: [object Module]
Your code should look like: const MyComponent = lazy(() =>
import('./MyComponent'))
at RenderedRoute (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:4840:5)
at Routes (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5272:5)
at Suspense
at App
at Router (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5204:15)
at BrowserRouter (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:3464:5)
overrideMethod # react_devtools_backend.js:4012 printWarning #
react.development.js:209 error # react.development.js:183
lazyInitializer # react.development.js:1400 resolveLazy #
react-dom.development.js:14907 reconcileSingleElement #
react-dom.development.js:15718 reconcileChildFibers #
react-dom.development.js:15808 reconcileChildren #
react-dom.development.js:19174 updateContextProvider #
react-dom.development.js:21154 beginWork #
react-dom.development.js:21649 beginWork$1 #
react-dom.development.js:27426 performUnitOfWork #
react-dom.development.js:26557 workLoopSync #
react-dom.development.js:26466 renderRootSync #
react-dom.development.js:26434 recoverFromConcurrentError #
react-dom.development.js:25850 performConcurrentWorkOnRoot #
react-dom.development.js:25750 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react_devtools_backend.js:4012
Warning: lazy: Expected the result of a dynamic import() call. Instead
received: [object Module]
Your code should look like: const MyComponent = lazy(() =>
import('./MyComponent'))
at Lazy
at RenderedRoute (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:4840:5)
at Routes (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5272:5)
at Suspense
at App
at Router (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5204:15)
at BrowserRouter (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:3464:5)
overrideMethod # react_devtools_backend.js:4012 printWarning #
react.development.js:209 error # react.development.js:183
lazyInitializer # react.development.js:1400 mountLazyComponent #
react-dom.development.js:19944 beginWork #
react-dom.development.js:21593 beginWork$1 #
react-dom.development.js:27426 performUnitOfWork #
react-dom.development.js:26557 workLoopSync #
react-dom.development.js:26466 renderRootSync #
react-dom.development.js:26434 recoverFromConcurrentError #
react-dom.development.js:25850 performConcurrentWorkOnRoot #
react-dom.development.js:25750 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react_devtools_backend.js:4012
Warning: lazy: Expected the result of a dynamic import() call. Instead
received: [object Module]
Your code should look like: const MyComponent = lazy(() =>
import('./MyComponent'))
at Lazy
at RenderedRoute (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:4840:5)
at Routes (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5272:5)
at Suspense
at App
at Router (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5204:15)
at BrowserRouter (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:3464:5)
overrideMethod # react_devtools_backend.js:4012 printWarning #
react.development.js:209 error # react.development.js:183
lazyInitializer # react.development.js:1400 mountLazyComponent #
react-dom.development.js:19944 beginWork #
react-dom.development.js:21593 callCallback #
react-dom.development.js:4164 invokeGuardedCallbackDev #
react-dom.development.js:4213 invokeGuardedCallback #
react-dom.development.js:4277 beginWork$1 #
react-dom.development.js:27451 performUnitOfWork #
react-dom.development.js:26557 workLoopSync #
react-dom.development.js:26466 renderRootSync #
react-dom.development.js:26434 recoverFromConcurrentError #
react-dom.development.js:25850 performConcurrentWorkOnRoot #
react-dom.development.js:25750 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react-dom.development.js:20013
Uncaught Error: Element type is invalid. Received a promise that
resolves to: undefined. Lazy element type must resolve to a class or
function.
at mountLazyComponent (react-dom.development.js:20013:1)
at beginWork (react-dom.development.js:21593:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
at invokeGuardedCallback (react-dom.development.js:4277:1)
at beginWork$1 (react-dom.development.js:27451:1)
at performUnitOfWork (react-dom.development.js:26557:1)
at workLoopSync (react-dom.development.js:26466:1)
at renderRootSync (react-dom.development.js:26434:1)
at recoverFromConcurrentError (react-dom.development.js:25850:1) mountLazyComponent # react-dom.development.js:20013 beginWork #
react-dom.development.js:21593 callCallback #
react-dom.development.js:4164 invokeGuardedCallbackDev #
react-dom.development.js:4213 invokeGuardedCallback #
react-dom.development.js:4277 beginWork$1 #
react-dom.development.js:27451 performUnitOfWork #
react-dom.development.js:26557 workLoopSync #
react-dom.development.js:26466 renderRootSync #
react-dom.development.js:26434 recoverFromConcurrentError #
react-dom.development.js:25850 performConcurrentWorkOnRoot #
react-dom.development.js:25750 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react_devtools_backend.js:4012 The
above error occurred in the <Route.Provider> component:
at RenderedRoute (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:4840:5)
at Routes (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5272:5)
at Suspense
at App
at Router (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:5204:15)
at BrowserRouter (http://localhost:4200/vendors-node_modules_react-router-dom_dist_index_js.js:3464:5)
Consider adding an error boundary to your tree to customize error
handling behavior. Visit https://reactjs.org/link/error-boundaries to
learn more about error boundaries. overrideMethod #
react_devtools_backend.js:4012 logCapturedError #
react-dom.development.js:18687 update.callback #
react-dom.development.js:18720 callCallback #
react-dom.development.js:13923 commitUpdateQueue #
react-dom.development.js:13944 commitLayoutEffectOnFiber #
react-dom.development.js:23391 commitLayoutMountEffects_complete #
react-dom.development.js:24688 commitLayoutEffects_begin #
react-dom.development.js:24674 commitLayoutEffects #
react-dom.development.js:24612 commitRootImpl #
react-dom.development.js:26823 commitRoot #
react-dom.development.js:26682 finishConcurrentRender #
react-dom.development.js:25892 performConcurrentWorkOnRoot #
react-dom.development.js:25809 workLoop # scheduler.development.js:266
flushWork # scheduler.development.js:239 performWorkUntilDeadline #
scheduler.development.js:533 queue. # task.js:61 run #
task.js:35 listener # task.js:46 react-dom.development.js:26923
Uncaught TypeError: Cannot read properties of undefined (reading
'displayName')
at getDisplayName (react_devtools_backend.js:261:19)
at getDisplayNameForFiber (react_devtools_backend.js:6381:55)
at Object.markComponentErrored (react_devtools_backend.js:5615:29)
at markComponentErrored (react-dom.development.js:5053:1)
at handleError (react-dom.development.js:26307:1)
at renderRootSync (react-dom.development.js:26437:1)
at recoverFromConcurrentError (react-dom.development.js:25850:1)
at performConcurrentWorkOnRoot (react-dom.development.js:25750:1)
at workLoop (scheduler.development.js:266:1)
at flushWork (scheduler.development.js:239:1)
What am I doing wrong or what's missing here? Is there an example how to use this documentation to combine React and Angular? If you do the same steps with only React or only Angular it works fine..
Hope someone can help..
Angular Micro Frontend:
Shell Application:
React Micro Frontend
I do not exactly know what is wrong on your configuration, but to use different technologies together with module-federation please take a look here:
https://www.angulararchitects.io/aktuelles/multi-framework-and-version-micro-frontends-with-module-federation-your-4-steps-guide/
There is a very good example how to combine different technologies together.
I hope that helps.

I have been trying to fetch an API recipepuppy.com and I keep getting this : Syntax Error and 500 Internal server error

I have used recipepuppy api and while fetching the items i'm unable to get the result and i keep on getting this error
the code is
search(){
let {ingredients, dish} = this.state;
const url =`http://www.recipepuppy.com/api/?i=${ingredients}&q=${dish}`;
fetch(url, {
method: 'GET'
}).then(response => response.json())
.then(json => console.log('recipes', json));
}
I'm getting this error in console as
SearchRecipes.js:17 GET http://www.recipepuppy.com/api/?i=garlic,chicken&q=adobo 500 (Internal Server Error)
search # SearchRecipes.js:17
onClick # SearchRecipes.js:49
callCallback # react-dom.development.js:189
invokeGuardedCallbackDev # react-dom.development.js:238
invokeGuardedCallback # react-dom.development.js:291
invokeGuardedCallbackAndCatchFirstError # react-dom.development.js:306
executeDispatch # react-dom.development.js:391
executeDispatchesInOrder # react-dom.development.js:416
executeDispatchesAndRelease # react-dom.development.js:3301
executeDispatchesAndReleaseTopLevel # react-dom.development.js:3310
forEachAccumulated # react-dom.development.js:3282
runEventsInBatch # react-dom.development.js:3327
runExtractedPluginEventsInBatch # react-dom.development.js:3537
handleTopLevel # react-dom.development.js:3581
batchedEventUpdates$1 # react-dom.development.js:21729
batchedEventUpdates # react-dom.development.js:798
dispatchEventForLegacyPluginEventSystem # react-dom.development.js:3591
attemptToDispatchEvent # react-dom.development.js:4311
dispatchEvent # react-dom.development.js:4232
unstable_runWithPriority # scheduler.development.js:659
runWithPriority$1 # react-dom.development.js:11077
discreteUpdates$1 # react-dom.development.js:21746
discreteUpdates # react-dom.development.js:811
dispatchDiscreteEvent # react-dom.development.js:4211
VM1141:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 91
please help me with this
The problem is not on your end, this API does not send the body data properly, and concatenates an HTML string onto a JSON response, hence you can't actually parse the response properly. If this isn't your API just contact the owner and let them know.

MERN (Axios): Uncaught TypeError (data is not defined)

I am new to MERN stack and I am trying to build a login/registration system on the stack.
When I am trying to make a post request it is throwing this error:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')
at registrationHandler (Register.jsx:47:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
at invokeGuardedCallback (react-dom.development.js:4056:1)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4070:1)
at executeDispatch (react-dom.development.js:8243:1)
at processDispatchQueueItemsInOrder (react-dom.development.js:8275:1)
at processDispatchQueue (react-dom.development.js:8288:1)
at dispatchEventsForPlugins (react-dom.development.js:8299:1)
at react-dom.development.js:8508:1
registrationHandler # Register.jsx:47
callCallback # react-dom.development.js:3945
invokeGuardedCallbackDev # react-dom.development.js:3994
invokeGuardedCallback # react-dom.development.js:4056
invokeGuardedCallbackAndCatchFirstError # react-dom.development.js:4070
executeDispatch # react-dom.development.js:8243
processDispatchQueueItemsInOrder # react-dom.development.js:8275
processDispatchQueue # react-dom.development.js:8288
dispatchEventsForPlugins # react-dom.development.js:8299
(anonymous) # react-dom.development.js:8508
batchedEventUpdates$1 # react-dom.development.js:22396
batchedEventUpdates # react-dom.development.js:3745
dispatchEventForPluginEventSystem # react-dom.development.js:8507
attemptToDispatchEvent # react-dom.development.js:6005
dispatchEvent # react-dom.development.js:5924
unstable_runWithPriority # scheduler.development.js:468
runWithPriority$1 # react-dom.development.js:11276
discreteUpdates$1 # react-dom.development.js:22413
discreteUpdates # react-dom.development.js:3756
dispatchDiscreteEvent # react-dom.development.js:5889
My code is as follows:
This is being called on Form Submit
When I am making the same request from Postman it happens successfully:
Please any help is appreciated...
I was importing Axios as
import { axios } from 'axios'
which should be
import axios from 'axios'.
Also, history.push() was replaced by useNavigate('/') in v6 of react-router-dom.
Lastly at line 30, it should be data.data.token.

process.env.REACT_APP_KEY not working with api calls

So far in 2 projects I created a .env file in the root of my project. I install npm install dotenv. I place (e.g.)REACT_APP_KEY='123456' in the env file . In my app.js file I add require("dotenv").config() in my component where I am making the api call i console.log(process.env.REACT_APP_KEY) in the the console I get 123456. So far it looks like its working. But when I place the process.env.REACT_APP_KEY in the api call I get 404 error? What am i missing? If I remove process.env.REACT_APP_KEY and add 123456, I get no error and the call works. Is it because I am using axios to get the data, should i use fetch? I can't seem to find anything that can help me with this. I included my error below.
thank you.
xhr.js:177 GET https://api.unsplash.com/search/photos?query=bali 401
dispatchXhrRequest # xhr.js:177
xhrAdapter # xhr.js:13
dispatchRequest # dispatchRequest.js:52
Promise.then (async)
request # Axios.js:61
Axios.<computed> # Axios.js:76
wrap # bind.js:9
getImages # App.js:12
onSearchSubmit # App.js:20
App # App.js:22
renderWithHooks # react-dom.development.js:14985
mountIndeterminateComponent # react-dom.development.js:17811
beginWork # react-dom.development.js:19049
beginWork$1 # react-dom.development.js:23940
performUnitOfWork # react-dom.development.js:22776
workLoopSync # react-dom.development.js:22707
renderRootSync # react-dom.development.js:22670
performSyncWorkOnRoot # react-dom.development.js:22293
scheduleUpdateOnFiber # react-dom.development.js:21881
updateContainer # react-dom.development.js:25482
(anonymous) # react-dom.development.js:26021
unbatchedUpdates # react-dom.development.js:22431
legacyRenderSubtreeIntoContainer # react-dom.development.js:26020
render # react-dom.development.js:26103
(anonymous) # index.js:7
./src/index.js # index.js:18
__webpack_require__ # bootstrap:856
fn # bootstrap:150
1 # reportWebVitals.js:14
__webpack_require__ # bootstrap:856
checkDeferredModules # bootstrap:45
webpackJsonpCallback # bootstrap:32
(anonymous) # main.chunk.js:1
createError.js:16 Uncaught (in promise) Error: Request failed with status code 401
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)

Uncaught (in promise) Error: Request failed with status code 400 when spring app is hosted on localhost

I am trying to access a spring boot micro-service hosting on localhost but I am getting following error:
Uncaught (in promise) Error: Request failed with status code 400
Following is my code to access the service:
import axios from "axios";
export const addProjectTask = (project_task, history) => async dispatch => {
await axios.post("http://localhost:8080/api/board", project_task);
history.push("/");
};
I have tried to search on the internet but I am not able to find any solution with the localhost. When I use the above URL in Postman it works fine so the URL is correct.
Edit:
Complete Error:
xhr.js:178 POST http://localhost:8080/api/board 400
dispatchXhrRequest # xhr.js:178
xhrAdapter # xhr.js:12
dispatchRequest # dispatchRequest.js:52
Promise.then (async)
request # Axios.js:61
Axios.<computed> # Axios.js:86
wrap # bind.js:9
(anonymous) # projectTaskActions.js:4
(anonymous) # index.js:8
(anonymous) # redux.js:477
onSubmit # AddProjectTask.js:31
callCallback # react-dom.development.js:188
invokeGuardedCallbackDev # react-dom.development.js:237
invokeGuardedCallback # react-dom.development.js:292
invokeGuardedCallbackAndCatchFirstError # react-dom.development.js:306
executeDispatch # react-dom.development.js:389
executeDispatchesInOrder # react-dom.development.js:414
executeDispatchesAndRelease # react-dom.development.js:3278
executeDispatchesAndReleaseTopLevel # react-dom.development.js:3287
forEachAccumulated # react-dom.development.js:3259
runEventsInBatch # react-dom.development.js:3304
runExtractedPluginEventsInBatch # react-dom.development.js:3514
handleTopLevel # react-dom.development.js:3558
batchedEventUpdates$1 # react-dom.development.js:21871
batchedEventUpdates # react-dom.development.js:795
dispatchEventForLegacyPluginEventSystem # react-dom.development.js:3568
attemptToDispatchEvent # react-dom.development.js:4267
dispatchEvent # react-dom.development.js:4189
unstable_runWithPriority # scheduler.development.js:653
runWithPriority$1 # react-dom.development.js:11039
discreteUpdates$1 # react-dom.development.js:21887
discreteUpdates # react-dom.development.js:806
dispatchDiscreteEvent # react-dom.development.js:4168

Resources