Strange behaviour

Here is a simple flow to use carbone.io template
image

the first inject node contains data

 {
     "entetes": [
         {
             "hdr": "col 1",
             "valeur": "Colonne 1"
         },
         {
             "hdr": "col 2",
             "valeur": "Colonne 2"
         },
         {
             "hdr": "col 3",
             "valeur": "Colonne 3"
         }
     ],
     "data": [
         {
             "hdr": [
                 {
                     "hdr": "col 1",
                     "valeur": "A 1"
                 },
                 {
                     "hdr": "col 2",
                     "valeur": "A 2"
                 },
                 {
                     "hdr": "col 3",
                     "valeur": "A 3"
                 }
             ]
         },
         {
             "hdr": [
                 {
                     "hdr": "col 1",
                     "valeur": "B 1"
                 },
                 {
                     "hdr": "col 2",
                     "valeur": "B 2"
                 },
                 {
                     "hdr": "col 3",
                     "valeur": "B 3"
                 }
             ]
         },
         {
             "hdr": [
                 {
                     "hdr": "col 1",
                     "valeur": "C 1"
                 },
                 {
                     "hdr": "col 2",
                     "valeur": "C 2"
                 },
                 {
                     "hdr": "col 3",
                     "valeur": "C 3"
                 }
             ]
         }
     ]
 }

in output, there is a buffer with 12510 bytes, and the conversion with carbone.io is ok
image

the second inject node does not contain data. But data are set in the function node

 const obj = {
     entetes:[
         {hdr:"col 1",valeur:"Colonne 1"},
         {hdr:"col 2",valeur:"Colonne 2"},
         {hdr:"col 3",valeur:"Colonne 3"}
     ],
     "data":[
         {hdr:[
             {hdr:"col 1",valeur:"A 1"},
             {hdr:"col 2",valeur:"A 2"},
             {hdr:"col 3",valeur:"A 3"}
         ]},
         {hdr:[
             {hdr:"col 1",valeur:"B 1"},
             {hdr:"col 2",valeur:"B 2"},
             {hdr:"col 3",valeur:"B 3"}
         ]},
         {hdr:[
             {hdr:"col 1",valeur:"C 1"},
             {hdr:"col 2",valeur:"C 2"},
             {hdr:"col 3",valeur:"C 3"}
         ]}
     ]
 };
 msg.payload = obj;
 return msg;

and now, the output is a buffer with 12044 bytes, and of course, the conversion with carbone.io does not work.
image

Any idea ? I am totally lost ! I do not understand why the same data provided in tow different ways does not give the same result !

Thanks for your help

Here is the complete flow

[
    {
        "id": "22f7a47b705ecce9",
        "type": "subflow",
        "name": "carbone.io",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 360,
                "y": 220,
                "wires": [
                    {
                        "id": "86a74d51bfadf18d"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 840,
                "y": 220,
                "wires": [
                    {
                        "id": "bec126f7ce06bed5",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "86a74d51bfadf18d",
        "type": "function",
        "z": "22f7a47b705ecce9",
        "name": "carbone.io",
        "func": "const carbone = global.get('carbone');\nlet donnees = msg.payload;\nconst template = msg.template;\n\n//carbone.render('.node-red/node_modules/carbone/examples/simple.docx', data, function (err, result) {\ncarbone.render(template, donnees, function (err, result) {\n    if (err) {\n        msg.payload = err;\n        node.send(msg);\n        console.log(err)\n        return msg;\n    }\n    // write the result\n    console.log(\"resultat \",result);\n    msg.payload = result;\n    node.send(msg);\n});\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 220,
        "wires": [
            [
                "bec126f7ce06bed5"
            ]
        ]
    },
    {
        "id": "bec126f7ce06bed5",
        "type": "file",
        "z": "22f7a47b705ecce9",
        "name": "",
        "filename": "filename",
        "filenameType": "msg",
        "appendNewline": false,
        "createDir": false,
        "overwriteFile": "true",
        "encoding": "none",
        "x": 700,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "61659a935ce4e0e3",
        "type": "comment",
        "z": "22f7a47b705ecce9",
        "name": "Contenu du msg",
        "info": "msg.payload : data\nmsg.template : template\nmsg.filename : filename",
        "x": 640,
        "y": 160,
        "wires": []
    },
    {
        "id": "d3fdf49a4cdea52c",
        "type": "subflow:22f7a47b705ecce9",
        "z": "d09f90d38227a234",
        "g": "4566950462049265",
        "name": "",
        "x": 670,
        "y": 680,
        "wires": [
            [
                "d6d9644d034ef075"
            ]
        ]
    },
    {
        "id": "55092a8cda98d291",
        "type": "inject",
        "z": "d09f90d38227a234",
        "g": "4566950462049265",
        "name": "with data",
        "props": [
            {
                "p": "template",
                "v": "C:\\Users\\Serge\\Documents\\#04 DB_SCENO\\Modeles\\carboneNomenclature.docx",
                "vt": "str"
            },
            {
                "p": "filename",
                "v": "C:\\Users\\Serge\\Documents\\#04 DB_SCENO\\Modeles\\carbone.docx",
                "vt": "str"
            },
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"entetes\":[{\"hdr\":\"col 1\",\"valeur\":\"Colonne 1\"},{\"hdr\":\"col 2\",\"valeur\":\"Colonne 2\"},{\"hdr\":\"col 3\",\"valeur\":\"Colonne 3\"}],\"data\":[{\"hdr\":[{\"hdr\":\"col 1\",\"valeur\":\"A 1\"},{\"hdr\":\"col 2\",\"valeur\":\"A 2\"},{\"hdr\":\"col 3\",\"valeur\":\"A 3\"}]},{\"hdr\":[{\"hdr\":\"col 1\",\"valeur\":\"B 1\"},{\"hdr\":\"col 2\",\"valeur\":\"B 2\"},{\"hdr\":\"col 3\",\"valeur\":\"B 3\"}]},{\"hdr\":[{\"hdr\":\"col 1\",\"valeur\":\"C 1\"},{\"hdr\":\"col 2\",\"valeur\":\"C 2\"},{\"hdr\":\"col 3\",\"valeur\":\"C 3\"}]}]}",
        "payloadType": "json",
        "x": 280,
        "y": 680,
        "wires": [
            [
                "d3fdf49a4cdea52c"
            ]
        ]
    },
    {
        "id": "f0b28ec8f335eb58",
        "type": "function",
        "z": "d09f90d38227a234",
        "g": "4566950462049265",
        "name": "function 3",
        "func": "const obj = {\n    entetes:[\n        {hdr:\"col 1\",valeur:\"Colonne 1\"},\n        {hdr:\"col 2\",valeur:\"Colonne 2\"},\n        {hdr:\"col 3\",valeur:\"Colonne 3\"}\n    ],\n    \"data\":[\n        {hdr:[\n            {hdr:\"col 1\",valeur:\"A 1\"},\n            {hdr:\"col 2\",valeur:\"A 2\"},\n            {hdr:\"col 3\",valeur:\"A 3\"}\n        ]},\n        {hdr:[\n            {hdr:\"col 1\",valeur:\"B 1\"},\n            {hdr:\"col 2\",valeur:\"B 2\"},\n            {hdr:\"col 3\",valeur:\"B 3\"}\n        ]},\n        {hdr:[\n            {hdr:\"col 1\",valeur:\"C 1\"},\n            {hdr:\"col 2\",valeur:\"C 2\"},\n            {hdr:\"col 3\",valeur:\"C 3\"}\n        ]}\n    ]\n};\nmsg.payload = obj;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 740,
        "wires": [
            [
                "d3fdf49a4cdea52c"
            ]
        ]
    },
    {
        "id": "3c08e9e4db56bae6",
        "type": "inject",
        "z": "d09f90d38227a234",
        "g": "4566950462049265",
        "name": "without data",
        "props": [
            {
                "p": "template",
                "v": "C:\\Users\\Serge\\Documents\\#04 DB_SCENO\\Modeles\\carboneNomenclature.docx",
                "vt": "str"
            },
            {
                "p": "filename",
                "v": "C:\\Users\\Serge\\Documents\\#04 DB_SCENO\\Modeles\\carbone.docx",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 290,
        "y": 740,
        "wires": [
            [
                "f0b28ec8f335eb58"
            ]
        ]
    }
]

You have this :

And not

 const obj = {
     "entetes":[
         {"hdr":"col 1","valeur":"Colonne 1"},

Additional " around the keys. I guess it explains why...

I am not savvy enough to explain the implication in terms of JSON.
But, the first one is not valid and passed to a json check.

That should not make any difference. In javascript the " are optional around keys without special characters. In the inject node they are required because that is not javascript it is JSON. If you feed the two into a debug node you will see the same output for the two paths.

By all means try adding the quotes in the function node, but it should not change anything.

Did you realise that the subflow will write to the file twice for one input message? In the function node carbone.io you have node.send(msg) inside each path in the callback, then you have return msg at the end. Probably you want to remove the return.

So :

  • with or without quotes, I have the same result as explained Colin
  • I removed "return msg" in the subflow "carbone.io"
    The result is the same !!!

We can't test it properly because the global get is undefined

Change all the backslashes in the filenames to forward slashes, though I don't actually think it is that.

if you want to test with carbone.io, just install it
npm install carbone

I found a solution :


I use a node json to convert JSON object to string, and then, in the subflow carbone.io, I use JSON.parse to convert string to object and it works perfectly !

data = JSON.parse(msg.payload)
...

Thanks for your help :slight_smile:

Actually what it is doing is converting the javascript object to a JSON string.

So converting the object to a JSON string and back to an object fixes it? Weird.