# Having issues with axios getting and returning data in my custom node

**URL:** https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252
**Category:** General
**Created:** [9 August 2022 21:14 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252 "2022-08-09T21:14:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![MyRandomThoughts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myrandomthoughts/32/41085_2.png) [@MyRandomThoughts](https://discourse.nodered.org/u/MyRandomThoughts)
#### Post date: [9 August 2022 21:14 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252/1 "2022-08-09T21:14:53Z")

</div>

I am trying to use axios to call a URL and return its data. That part is working, but the ordering is not working when used as a function call. I am sure it's something simple, but I am not used to JavaScript.

The code below has two `node.warn` parts so that I can see what is being returned.  
The one in the function `node.warn(["Finally", r, e])` shows the correct data, the one after the function call `node.warn(["HTTP", httpReq]);` is returned first, and shows `httpReq` as being undefined. It's like the code is not waiting for the function call to execute.

```auto
function httpRequest(method, url, headers) {
    var r = null;
    var e = null;

    switch (method) {
        case "GET": {
            axios.get(url)
                .then(response => {
                    r = response.data;
                })
                .catch(error => {
                    e = error;
                })
                .finally(() => {
                    node.warn(["Finally", r, e])
                    return [r, e];
                })
            break;
        }

        case "POST": {
            node.warn("POST");
            break;
        }

        default: {
            raiseError("No method defined", msg);
            break;
        }
    }
}

```

With the function call of

```auto
this.on('input', function(msg, send, done) {
    if ((this.action != "Log Off") && (!flow.sid || flow.sid == undefined)) {
        var url = "http://www.example.com"
        var httpReq = httpRequest("GET", url);
        node.warn(["HTTP", httpReq]);
    }
    done();
})

```

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [9 August 2022 22:34 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252/2 "2022-08-09T22:34:09Z")

</div>

Just a question first - many people who are new to Node Red bring their "old way" of doing things - wondering have you looked at the HTTP request node to do this for you ? i.e. move your code into the visual paradigm or is there a reason this will not work for you ?

Craig

---

<div class="post-metadata">

### Author: ![MyRandomThoughts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myrandomthoughts/32/41085_2.png) [@MyRandomThoughts](https://discourse.nodered.org/u/MyRandomThoughts)
#### Post date: [9 August 2022 22:43 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252/3 "2022-08-09T22:43:54Z")

</div>

I have a working subflow performing one specific API call but want to expand the functionality to cover more API calls. The only way to do this without it being a massively huge flow is by creating a custom node

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [10 August 2022 00:05 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252/4 "2022-08-10T00:05:17Z")

</div>

Aaah right - fair enough i guess - i personally would still go with inidividual http nodes unless you are talking 100's of api calls

Craig

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [10 August 2022 05:44 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252/5 "2022-08-10T05:44:27Z")

</div>

> [@MyRandomThoughts](#):
>
> It's like the code is not waiting for the function call to execute.

you are right .. because axios is **async** it executes in your httpRequest function and then it moves along to the next thing .. which is the **break**? it doesnt return anything at that point

maybe it would be better if you `return` the axios call (which is a Promise)

```auto
function httpRequest(method, url, headers) {

      switch (method) {
        case "GET": {
          return axios.get(url);
        }

        case "POST": {
          node.warn("POST");
        }

        default: {
          raiseError("No method defined", msg);
          break;
        }
      }
    }

```

... and then use `await` and a try catch on the `input` callback (which needs to be `async` also)

```javascript
   this.on("input", async function (msg, send, done) {
      var url = "http://www.example.com";

      try {
        var response = await httpRequest("GET", url);
        var httpReq = response.data;
        node.send({ payload: httpReq });
      } catch (err) {
        node.warn(err);
      }
      done();
    });

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [10 August 2022 05:48 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252/6 "2022-08-10T05:48:22Z")

</div>

> [@MyRandomThoughts](#):
>
> `node.warn(["HTTP", httpReq]);` is returned first

This is the nature of the async beast.

Add a callback argument to your `httpRequest` function & call the callback inside the `then` and `catch` (get rid of the two variables & the `finally`)

---

<div class="post-metadata">

### Author: ![MyRandomThoughts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myrandomthoughts/32/41085_2.png) [@MyRandomThoughts](https://discourse.nodered.org/u/MyRandomThoughts)
#### Post date: [10 August 2022 08:08 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252/7 "2022-08-10T08:08:20Z")

</div>

Thanks, I'll have a play

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [24 August 2022 08:08 UTC](https://discourse.nodered.org/t/having-issues-with-axios-getting-and-returning-data-in-my-custom-node/66252/8 "2022-08-24T08:08:24Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
