0

Edit: I don't ask anything about cors here

So I have spent hours trying to figure out what's goin on, and well ... I'm losing it...

So I have this simple app that has the server part made in nodejs and the front end in react, they run on diferent ports so I used cors module.

Anyway So this is my nodejs api using express

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.get('/', (req, res) => {
  res.send({data:"Hello"});
});
app.listen(port, () => console.log(`Listening on port ${port}`));

and then from react the following fetch request

    fetch("http://www.localhost:5000/",{
        method: 'GET',
        mode: "no-cors",
        cache: "no-cache", 
        credentials: "same-origin", 
        headers: {"Content-Type": "application/json"}
      })
    .then(res=>{
        console.log("res1",res)
        res.text();
    })
    .then(res=>{
        console.log("res2",res)
    })
    .catch(res=>{
        console.log("Exception : ",res);
    })

The thing is this req returns undefined whatever I try. In res 1 the response is an object ( in this case opaque )

Response {type: "opaque", url: "", redirected: false, status: 0, ok: false, …}

And well res2 is undefined.

What am I missing ?

Ionut Eugen
  • 481
  • 2
  • 6
  • 27
  • 2
    You need to return a value for each `then` call for it to be passed down. – Cameron Downer May 27 '19 at 23:17
  • 2
    if you are not asking about CORS, why have you tagged your question so? Tag only with what you are asking *about*, not with what your question *contains* – YakovL May 28 '19 at 17:20

1 Answers1

0
fetch("http://www.localhost:5000/",{
        method: 'GET',
        mode: "no-cors",
        cache: "no-cache", 
        credentials: "same-origin", 
        headers: {"Content-Type": "application/json"}
      })
    .then(res=>{
        console.log("res1",res)
        return res.text();
    })
    .then(res=>{
        console.log("res2",res)

    })
    .catch(res=>{
        console.log("Exception : ",res);
    })
noor
  • 1,611
  • 16
  • 16
  • Thank you.. I'm tired .. and maybe stupid ... my question was stupid ... apologies – Ionut Eugen May 27 '19 at 23:20
  • 1
    All good. No question is stupid here. Happy to help. Please do mark answer as accepted so that others benefit from it. – noor May 27 '19 at 23:22