# Error while adding a new flow using the API

**URL:** https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729
**Category:** General
**Created:** [9 November 2018 20:51 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729 "2018-11-09T20:51:49Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [9 November 2018 20:51 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/1 "2018-11-09T20:51:49Z")

</div>

Hi,

I am trying to add a flow which has **two**  **connected** nodes (`nodeType1` and `nodeType2`). Following is my code  
`

```
     var nodes = [
                 {
                    "id":"5554e61.aff8018",
                    "label":"AppInstance1"
                },
                {
                    "id":"759b90c5.2a17b",
                    "type":"nodeType1",
                    "z":"5554e61.aff8018"
                },
                {
                    "id":"a91c4c.f99c93b8",
                    "type":"nodeType2",
                    "z":"5554e61.aff8018",
                    "wires":[
                        [
                           "759b90c5.2a17b"
                        ]
                    ]
                }];`

```

`

```
    let flowDescription = {
                "id": "o2G75uz.5ceKyNF",
                "label": "Sheet12345",
                "nodes": nodes,
                "configs": [].toString()
    };`
            
    $.ajax({
               type: "POST",
                url: "http://127.0.0.1:1880/flow",
                data: flowDescription,
                dataType: "json"
            });`

```

I am getting an error notification saying that cannot add the flow due to missing nodes but the missing nodes list it empty. (note: `nodeType1` and `nodeType2` node have been installed).

1. Are these node descriptions correct? If not what is the issue?
2. Can I add wirings in between nodes as I have added into the node descriptions?

Appreciate your inputs.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [9 November 2018 23:04 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/2 "2018-11-09T23:04:59Z")

</div>

The first node in your nodes array has an id and label, but no type.... That isn't a valid node. I assume it's your tab... Which should not be in the nodes array as it isn't a node. Those properties are set via the properties you have in flowDescription.

Why are you doing `[].to string()` when it should be an array not a string?

---

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [13 November 2018 11:16 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/3 "2018-11-13T11:16:44Z")

</div>

Hi.

I corrected the issue in the payload.

I used`"configs".toString()` because the AJAX request had removed empty array while sending the request. Anyway, I removed that string conversion.

Now I am trying to create a flow with 1 inject node connected to 1 debug node. Following is the part of the code which prepares the payload for the request.

```
 function createFlow() {
        let head = makeid(7);
        let tail = makeid(7);
        let flowId = head + '.' + tail;
        let nodes = [

            {
                "id":"d2b19215.deabd",
                "type":"inject",
                "name":"",
                "topic":"",
                "payload":"",
                "payloadType":"date",
                "repeat":"",
                "crontab":"",
                "once":false,
                "onceDelay":0.1,
                "x":100,
                "y":100,
                "wires":[
                    [
                        "1853e36g.1bdf8d"
                    ]
                ]
            },
            {
                "id":"1853e36g.1bdf8d",
                "type":"debug",
                "name":"",
                "active":true,
                "tosidebar":true,
                "console":false,
                "tostatus":false,
                "complete":"false",
                "x":330,
                "y":140,
                "wires":[

                ]
            }
    ];
    let flowDescription = {
        "id": flowId,
        "label": "Sheet123467",
        "nodes": nodes,
        "configs": []
    };
    $.ajax({
        type: "POST",
        url: "http://127.0.0.1:1880/flow",
        data: flowDescription,
        dataType: "json"
    });

```

I am not getting any error in the node-red backend end console and I can see the following message when I add the flow.

`13 Nov 12:01:45 - [info] Adding flow: Sheet1234 [7eaeb2d6.e3657c]`

Then the UI gives me a pop-to review and merge the changes. In that pop-up, I can click the merge button but it doesn't close with the merge button click. When I click review changes I can see the following result.

 ![Screenshot%20from%202018-11-13%2012-03-09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/56060fdc814980beb64886825c4d0f0bc7806ecc.png)

There I cannot merge as the merge button is not enabled. I can see only the inject node after I click the

 ![Screenshot%20from%202018-11-13%2012-03-34](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fa57a686fcd51fd85c9a442d4c1712a206c8b7fa.png) cancel button

Is there any issue in my flow description payload?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 November 2018 11:26 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/4 "2018-11-13T11:26:34Z")

</div>

Not sure - if you had added a new flow that the editor wasn't aware of, the first column in the diff table would be blank for that flow. But that diff view suggests the editor already knew about flow Sheet1234.

It could be a bug with the diff tool not properly understanding what you're doing - hard to say. Would need to try to recreate it locally to see what's going on - so any more details on what you had to start with and what you added would be helpful.

---

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [13 November 2018 11:38 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/5 "2018-11-13T11:38:34Z")

</div>

Thanks for the response.

I added my full function to the code which I used to create a flow.

Although I give a never exist flow name to the new flow, the diff tool recognizes it as a changing of already existing flow.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 November 2018 11:42 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/6 "2018-11-13T11:42:01Z")

</div>

> [@malinthasa](#):
>
> Although I give a never exist flow name to the new flow

Which API are you using?

You should be using `POST /flow` to add the new flow configuration and the flow will be assigned a new id.

If you are using `POST /flow/:id` - that is for updating an existing flow.

---

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [13 November 2018 11:44 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/7 "2018-11-13T11:44:08Z")

</div>

I am using `POST /flow` endpoint

```
 $.ajax({
    type: "POST",
    url: "http://127.0.0.1:1880/flow",
    data: flowDescription,
    dataType: "json"
});
```

---

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [13 November 2018 11:45 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/8 "2018-11-13T11:45:41Z")

</div>

I can see the debug node in the created flow description although it is not displayed on UI

```
> [  
> {  
> "type":"tab",
> "label":"Sheet123467",
> "id":"bf354b13.d4e7d8"
> },
> {  
> "id":"d2b19215.deabd",
> "type":"inject",
> "name":"",
> "topic":"",
> "payload":"",
> "payloadType":"date",
> "repeat":"",
> "crontab":"",
> "once":"false",
> "onceDelay":"0.1",
> "x":"100",
> "y":"100",
> "wires":[  
> [  
> "1853e36g.1bdf8d"
> ]
> ],
> "z":"bf354b13.d4e7d8"
> },
> {  
> "id":"1853e36g.1bdf8d",
> "type":"debug",
> "name":"",
> "active":"true",
> "tosidebar":"true",
> "console":"false",
> "tostatus":"false",
> "complete":"false",
> "x":"330",
> "y":"140",
> "z":"bf354b13.d4e7d8"
> }
> ]
```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 November 2018 12:39 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/9 "2018-11-13T12:39:59Z")

</div>

Forget about the notification you get in the editor - that's a separate problem.

For now, just reload the editor to get the latest flows displayed. Do the nodes you've added appear as you expect?

---

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [13 November 2018 12:53 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/10 "2018-11-13T12:53:10Z")

</div>

No, after manual reload it shows only the inject node with a super scripted `"1"`. This is a screenshot of that.

 ![Screenshot%20from%202018-11-13%2012-03-34](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fa57a686fcd51fd85c9a442d4c1712a206c8b7fa.png)

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 November 2018 12:55 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/11 "2018-11-13T12:55:55Z")

</div>

Do you have any debug nodes on Flow 1? What IDs do they have?

---

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [13 November 2018 12:59 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/12 "2018-11-13T12:59:02Z")

</div>

I don't have any nodes in Flow 1. This is the flow description after adding the new flow

```
[  
   {  
      "id":"17977523.10e47b",
      "type":"tab",
      "label":"Flow 1",
      "disabled":false,
      "info":""
   },
   {  
      "type":"tab",
      "label":"Sheet123467",
      "id":"9acb1a7f.e903a8"
   },
   {  
      "id":"d2b19215.deabd",
      "type":"inject",
      "name":"",
      "topic":"",
      "payload":"",
      "payloadType":"date",
      "repeat":"",
      "crontab":"",
      "once":"false",
      "onceDelay":"0.1",
      "x":"100",
      "y":"100",
      "wires":[  
         [  
            "1853e36g.1bdf8d"
         ]
      ],
      "z":"9acb1a7f.e903a8"
   },
   {  
      "id":"1853e36g.1bdf8d",
      "type":"debug",
      "name":"",
      "active":"true",
      "tosidebar":"true",
      "console":"false",
      "tostatus":"false",
      "complete":"false",
      "x":"330",
      "y":"140",
      "z":"9acb1a7f.e903a8"
   }
]
```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 November 2018 13:17 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/13 "2018-11-13T13:17:52Z")

</div>

Any errors in the javascript console when you load that flow in the editor?

---

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [13 November 2018 13:20 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/14 "2018-11-13T13:20:53Z")

</div>

Ops, yes I am getting following error in the browser console.

```
    red.min.js:16 Uncaught TypeError: Cannot read property 'length' of undefined
    at Object.d [as import] (red.min.js:16)
    at Object.success (main.min.js:16)
    at j (vendor.js:2)
    at Object.fireWith [as resolveWith] (vendor.js:2)
    at x (vendor.js:5)
    at XMLHttpRequest.b (vendor.js:5)
```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 November 2018 13:25 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/15 "2018-11-13T13:25:27Z")

</div>

Ok - this has uncovered some issues in the import code in the editor as your flow isn't 100% valid.

1. Your Debug node should have a `wires` property set to an empty array.
2. Your X and Y properties must be Numbers not Strings

I've fixed in git master the handling of both of these cases so it'll work in the future. But for now, you'll need to make these changes in the flow you're trying to add.

---

<div class="post-metadata">

### Author: ![malinthasa](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@malinthasa](https://discourse.nodered.org/u/malinthasa)
#### Post date: [13 November 2018 13:33 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/16 "2018-11-13T13:33:27Z")

</div>

I think there is a confusion between the **flow description** and my **payload** to the API. That payload doesn't have the two issues you have mentioned above. Following is my payload to the API which still gives the same issue

> [@malinthasa](#):
>
> ```auto
> function createFlow() {
> let head = makeid(7);
> let tail = makeid(7);
> let flowId = head + '.' + tail;
> let nodes = [
> 
> {
> "id":"d2b19215.deabd",
> "type":"inject",
> "name":"",
> "topic":"",
> "payload":"",
> "payloadType":"date",
> "repeat":"",
> "crontab":"",
> "once":false,
> "onceDelay":0.1,
> "x":100,
> "y":100,
> "wires":[
> [
> "1853e36g.1bdf8d"
> ]
> ]
> },
> {
> "id":"1853e36g.1bdf8d",
> "type":"debug",
> "name":"",
> "active":true,
> "tosidebar":true,
> "console":false,
> "tostatus":false,
> "complete":"false",
> "x":330,
> "y":140,
> "wires":[
> 
> ]
> }
> ];
> let flowDescription = {
> "id": flowId,
> "label": "Sheet123467",
> "nodes": nodes,
> "configs": []
> };
> $.ajax({
> type: "POST",
> url: "http://127.0.0.1:1880/flow",
> data: flowDescription,
> dataType: "json"
> });
> 
> ```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 November 2018 14:03 UTC](https://discourse.nodered.org/t/error-while-adding-a-new-flow-using-the-api/4729/17 "2018-11-13T14:03:32Z")

</div>

The problem is with how you're sending the request.

> [@malinthasa](#):
>
> ```auto
> $.ajax({
> type: "POST",
> url: "http://127.0.0.1:1880/flow",
> data: flowDescription,
> dataType: "json" 
> });
> 
> ```

To send a JSON payload in a request with jQuery you need to:

1. stringify the `data` property - if you give it an object, it'll form encode it which is causing the empty `wires` array of your Debug node to be lost.
2. set content-type properly - the `dataType` property tells jquery how to handle the response, not how to encode the request.

```auto
 $.ajax({
        type: "POST",
        url: "/flow",
        data: JSON.stringify(flowDescription),
        dataType: "json",
        contentType:"application/json"
})

```

I'm sure there are things we could do on the server side to be more resilient to this sorts of mistakes.
