Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to get data from express to react

I am trying to fetch data from express to react using fetch api. Both are being run on localhost.
Here’s my backend code :

 app.get("/profile",(req,res)=>{
   const data = [{
     name:"shekhar",
     class:"5"
   }]
   res.send(JSON.stringify(data));
 });

 React code:
 
 function handleSubmit(){
   fetch("http://localhost:4000/profile")
   .then((response)=>{
     console.log((response));
   })
   .catch(err=>{console.log(err)})
 }
 
 handleSubmit();
 function handleSubmit(){
   fetch("http://localhost:4000/profile")
   .then((response)=>{
     console.log((response));
   })
   .catch(err=>{console.log(err)})
 }
 
 handleSubmit();
 
 The result it is showing in react console:
 Response {type: 'cors', url: 'http://localhost:4000/profile', redirected: false, status: 200, ok: true, …}


But its not showing the express object. but it is showing a response object. I have tried several answers over here but I couldn’t resolve the issue. Kindly help me what to do I am new to this. ty

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It will return an promise not an actual data, to get an actual data you need to resolve promise like this

fetch("http://localhost:4000/profile")
.then(response => response.json())
.then(data => {
 console.log(data);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading