Error while adding a new flow using the API

#1

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.

0 Likes

#2

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?

0 Likes

#3

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.

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

cancel button

Is there any issue in my flow description payload?

0 Likes

#4

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.

0 Likes

#5

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.

0 Likes

#6

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.

0 Likes

#7

I am using POST /flow endpoint

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

#8

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"
>    }
> ]
0 Likes

#9

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?

0 Likes

#10

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

0 Likes

#11

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

0 Likes

#12

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"
   }
]
0 Likes

#13

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

0 Likes

#14

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)
0 Likes

#15

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.

1 Like

#16

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

0 Likes

#17

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

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.
 $.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.

0 Likes