3

I have the following code but I get no console output. It seems like no matter what I do it doesn't enter the then() or catch().

axios
  .post(url, {test: "0"})
  .then(console.log)       
  .catch(console.error);

Interestingly enough the request gets posted and is received on the other end. I can also verify by mocking with postman that the endpoint is responding correctly. Where could this fail?

EDIT: It is getting even weirder: I tried the following and it instantly prints the promise:

console.log(await axios.post(url, {test: "0"}));

Unfortunately I am forced to use Node v8.11.1 - could this be the issue?

EDIT 2: .then(console.log) is just another way of writing .then(response => console.log(response)) but just to be sure I went to the official doc and used the recommend way as well:

axios
  .post(url, {test: "0"})
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

The problem persists.

leonheess
  • 16,068
  • 14
  • 77
  • 112
  • `.then((response) => console.log(response))` over here is failing and do the same thing with the catch too. – Nicolae Maties Dec 09 '19 at 14:08
  • 7
    @NicolaeMaties There is no difference. – Kobe Dec 09 '19 at 14:10
  • @NicolaeMaties Why? AFAIK a direct invocation is totally legal here – leonheess Dec 09 '19 at 14:11
  • Try the code [here](https://github.com/axios/axios), under the heading "Performing a POST Request." – Robert Harvey Dec 09 '19 at 14:12
  • 4
    Isn't the console Object overwritten by something? You could try putting debugger statements in callbacks provided to then and catch to see if any of them are invoked (`.then(response => {debugger;})`). Also, you can check out the return value of the post function directly, to see if it is a Promise or not – bgazsi Dec 09 '19 at 14:14
  • 2
    Just for the records, have you checked if it's the CORS problem? – Mirakurun Dec 09 '19 at 14:19
  • @RobertHarvey It's basically the same but I tried it out just in case and same behavior – leonheess Dec 09 '19 at 14:22
  • @bgazsi I tried that but it doesn't even seem to enter the `then()` so the bugger isn't started – leonheess Dec 09 '19 at 14:27
  • @leonheess If the `url` in your request is different from where you are sending the request (e.g. website is on localhost and api is hosted somewhere) you have to enable the [Cross-origin Resource Sharing](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) by adding the header to your API `"Access-Control-Allow-Origin": "your domain"` – Mirakurun Dec 09 '19 at 14:28
  • @Mirakurun Good idea but I set CORS to * and it didn't change anything :/ – leonheess Dec 09 '19 at 14:48
  • Are you sure the url is not null or invalid? – Vasileios Pallas Dec 09 '19 at 15:32
  • @VassilisPallas Quoting my own question: *Interestingly enough the request gets posted and is received on the other end.* – leonheess Dec 09 '19 at 15:59

1 Answers1

0

Not a problem of Axios at all. Azure killed my process immediately after the POST and didn't wait for the Promise to resolve. In case someone has a similar problem, see Promise is not working in azure function app javascript

leonheess
  • 16,068
  • 14
  • 77
  • 112