0

I am new to promises back-end in general, so apologies if this is a dumb question. receivePublicToken() is a method being called from a REST endpoint. I am then trying to call an axios function within that method's then method (aka, if the receivePublicToken() method succeeds, continue on and call another axios function). The axios function always returns an error.

import { receivePublicToken } from "./controller";    

receivePublicToken(req)
.then(resolve => {
  console.log("Resolved: ", resolve);
  axios.get("/transactions")
  .then(res => console.log("Axios succeeded!"))
  .catch(rej => console.log("Axios failed!")); //this always fails
})
.catch(reject => {
  console.log("Reject: ", reject);
});

The error it gives me:

{ Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)

Any thoughts? Alternatively, if anyone has any suggestions on a better approach to this, I'm all ears. Thank you to all in advance! :)

Ben
  • 317
  • 2
  • 5
  • 10
  • 1
    Doesn't look like anything to do with Promises or the JS here, it looks like a connectivity issue – CertainPerformance Feb 18 '20 at 02:46
  • @CertainPerformance That's what I thought too, except that my first REST endpoint is called via an axios call, so connection appears to be working fine in the beginning. – Ben Feb 18 '20 at 02:48
  • 1
    `receivePublicToken`...Is that you have to do the `get` request with the token in the header? – Andus Feb 18 '20 at 02:55
  • @Andus Sorry, what? – Ben Feb 18 '20 at 02:57
  • According to your code you want the request to be done after receiving some kind of token, my guess is a refused connection might because of the missing token in the request. I'm not sure but see if this helps you: https://stackoverflow.com/questions/41519092/using-axios-get-with-authorization-header-in-react-native-app – Andus Feb 18 '20 at 02:59
  • @Andus The function my get request is calling currently does not require any parameters, so I don't think that's the issue. – Ben Feb 18 '20 at 03:25
  • Does the get request work in postman alone? – Andus Feb 18 '20 at 03:27
  • @Andus Yessir, sure does – Ben Feb 18 '20 at 03:34

1 Answers1

0

If your API is implemented in a way that requires a secret token in the header then you probably need to insert the token in the header.

Can you provide a bit more detail about the get request you are executing?

is this line console.log("Resolved: ", resolve); executes?

  • For the purpose of getting this to work, my get request currently calls a method that just executes a console.log statement stating it reached that point. Later I will implement more logic in it, but I first want to get it working. And yes, my console.log("Resolved: ", resolve) line does execute. – Ben Feb 18 '20 at 03:24
  • Can you provide more details about your API implementation? And can you try sending a get request on the "/transactions" route directly instead of calling it inside first request? is it working without an issue? – Rajendrasinh Parmar Feb 19 '20 at 06:01