# No output from function node?

**URL:** <https://discourse.nodered.org/t/no-output-from-function-node/57054>\
**Category:** General\
**Created:** [22 January 2022 14:14 UTC](https://discourse.nodered.org/t/no-output-from-function-node/57054 "2022-01-22T14:14:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shark](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Shark](https://discourse.nodered.org/u/Shark)\
**Post date:** [22 January 2022 14:14 UTC](https://discourse.nodered.org/t/no-output-from-function-node/57054/1 "2022-01-22T14:14:54Z")

</div>

Hello everyone, I"m currently working on a project where we do some querries from a AWS DynamoDB Database. I'm currently working on it and managed to print everything I want on the console, but once I want to pass it through a msg.payload node, I can't manage to do it.  
So I'm not sure why my "Query on SensorTalbe" node is not returning anything...

```auto
[
    {
        "id": "384d5a3110ff57b6",
        "type": "tab",
        "label": "DynamoDB",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "72e62fd0ba30be05",
        "type": "ui_date_picker",
        "z": "384d5a3110ff57b6",
        "name": "",
        "label": "Date picker",
        "group": "8488dbd9c1c91b57",
        "order": 2,
        "width": 0,
        "height": 0,
        "passthru": true,
        "topic": "topic",
        "topicType": "msg",
        "className": "",
        "x": 130,
        "y": 280,
        "wires": [
            [
                "075bc6787001c562"
            ]
        ]
    },
    {
        "id": "075bc6787001c562",
        "type": "function",
        "z": "384d5a3110ff57b6",
        "name": "Query on SensorDataTable",
        "func": "var Time = msg.payload;\n\nfunction toDate(date) {\n if (date === void 0) {\n return new Date(0);\n }\n if (isDate(date)) {\n return date;\n } else {\n return new Date(parseFloat(date.toString()));\n }\n}\n\nfunction isDate(date) {\n return (date instanceof Date);\n}\n\nfunction format(date, format) {\n var d = toDate(date);\n return format\n .replace(/Y/gm, d.getFullYear().toString())\n .replace(/m/gm, ('0' + (d.getMonth() + 1)).substr(-2))\n .replace(/d/gm, ('0' + (d.getDate() + 1)).substr(-2))\n .replace(/H/gm, ('0' + (d.getHours() + 0)).substr(-2))\n .replace(/i/gm, ('0' + (d.getMinutes() + 0)).substr(-2))\n .replace(/s/gm, ('0' + (d.getSeconds() + 0)).substr(-2))\n .replace(/v/gm, ('0000' + (d.getMilliseconds() % 1000)).substr(-3));\n}\n\nlet CurrentDate = + new Date(); //number\nlet SpecifiedDate= + new Date(Time); //number\n\n\nvar dateFormat = \"Y-m-d H:i:s.v\";\n\nvar CurrentDateFormat = format(CurrentDate, dateFormat);\nvar SpecifiedDateFormat = format(SpecifiedDate, dateFormat);\n\nconsole.log(\"Between:\", SpecifiedDateFormat, \"and\", CurrentDateFormat);\n\nvar AWS = global.get('sdk');\n\nAWS.config.update({ region: 'us-east-2' });\n\nvar docClient = new AWS.DynamoDB.DocumentClient();\n\nconsole.log(\"Querying for events.\");\n\nvar params = {\n TableName : \"SensorDataTable\",\n KeyConditionExpression: \"#edt= :yyyy\",\n ExpressionAttributeNames:{\n \"#edt\": \"EventDateTime\"\n },\n FilterExpression: \"#edt between :start_edt and :end_edt\",\n ExpressionAttributeValues: {\n \":start_edt\": SpecifiedDateFormat, \n \":end_edt\": CurrentDateFormat\n }\n};\n\nconsole.log(\"Scanning EventDateTimes\");\n\ndocClient.scan(params, onScan);\n\nfunction onScan(err, data) {\n if (err) {\n console.error(\"Unable to scan the table. Error JSON:\", JSON.stringify(err, null, 2));\n } else {\n console.log(\"Scan succeeded.\");\n data.Items.forEach(function(item) {\n console.log(\n item.EventDateTime + \" Devide ID:\",\n item.DeviceID,\n \"- Temperature:\", item.Temperature,\n \"- Humidity:\", item.Humidity,\n \"- Mouvement:\", item.Mouvement,\n \"- UltraViolet:\", item.UltraViolet);\n\n payload = {\"Time\" :item.EventDateTime,\n \"ID\":item.DeviceID,\n \"Temperature\":item.Temperature,\n \"Humidity\":item.Humidity,\n \"Mouvement\":item.Mouvement,\n \"UltraViolet\": item.UltraViolet};\n \n msg.payload = payload;\n return msg;\n });\n\n }\n}\n\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 360,
        "y": 280,
        "wires": [
            [
                "6cbc2240c6325847"
            ]
        ]
    },
    {
        "id": "6cbc2240c6325847",
        "type": "debug",
        "z": "384d5a3110ff57b6",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 660,
        "y": 380,
        "wires": []
    },
    {
        "id": "8488dbd9c1c91b57",
        "type": "ui_group",
        "name": "Actionneurs",
        "tab": "e55ffb8a26591b17",
        "order": 4,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "e55ffb8a26591b17",
        "type": "ui_tab",
        "name": "RaspBerry #1",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

```

---

<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:** [22 January 2022 14:18 UTC](https://discourse.nodered.org/t/no-output-from-function-node/57054/2 "2022-01-22T14:18:57Z")

</div>

You need to return the msg from the function node (at top level scope) (not just return the msg inside a function inside the function node)

e.g. change `docClient.scan(params, onScan);` to `return docClient.scan(params, onScan);`

(Assuming that is the main process)

---

<div class="post-metadata">

**Author:** ![Shark](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Shark](https://discourse.nodered.org/u/Shark)\
**Post date:** [22 January 2022 15:07 UTC](https://discourse.nodered.org/t/no-output-from-function-node/57054/3 "2022-01-22T15:07:50Z")

</div>

Thanks for the quick answer !  
I'm new to Node-RED and to node.js in general.  
I think I understand what you told me. What I was doing previously was that the return I was trying to do was trapped inside the function "onScan". But if I just change `docClient.scan(params, onScan);` to `return docClient.scan(params, onScan);` don't I need a return somewhere to specify what I need to get out of the function ?  
Something like that (it's not working right now) :

```auto
return docClient.scan(params, onScan);

function onScan(err, data) {

    if (err) {
        console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Scan succeeded.");
        data.Items.forEach(function(item) {
            console.log(
                item.EventDateTime + " Devide ID:",
                item.DeviceID,
                "- Temperature:", item.Temperature,
                "- Humidity:", item.Humidity,
                "- Mouvement:", item.Mouvement,
                "- UltraViolet:", item.UltraViolet);
            
            var Temp = item.EventDateTime;
            payload = {Temp};
        
        return msg.payload;
        });
    }
}

```

---

<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:** [22 January 2022 15:44 UTC](https://discourse.nodered.org/t/no-output-from-function-node/57054/4 "2022-01-22T15:44:22Z")

</div>

> [@Shark](#):
>
> I need a return somewhere to specify what I need to get out of the function ?

- `return` returns something/nothing from every function
- the function node its self is a function
  - e.g. whatever code you enter gets wrapped in `function(node, msg, etc, etc) { YOUR FUNC CODE }`

- every function node must return an object [Reference](https://nodered.org/docs/user-guide/writing-functions)
- if you are doing async operations, you instead do `node.send(msg)`[Reference](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)

Looking closer at your function code, I see you have a callback `onScan` - this is async  
So inside `onScan` where you attempt to `return msg;` you should instead do `node.send(msg);`

@Shark  
UPDATE - try changing the `onScan` function to this...

```auto
function onScan(err, data) {
    if (err) {
        node.error(err, msg);
    } else {
        node.log("Scan succeeded.");
        msg.payload = data.Items;
        node.send(msg)
    }
}

```

---

<div class="post-metadata">

**Author:** ![Shark](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Shark](https://discourse.nodered.org/u/Shark)\
**Post date:** [22 January 2022 16:16 UTC](https://discourse.nodered.org/t/no-output-from-function-node/57054/5 "2022-01-22T16:16:36Z")

</div>

Thanks that was the solution !

---

<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:** [5 February 2022 16:17 UTC](https://discourse.nodered.org/t/no-output-from-function-node/57054/6 "2022-02-05T16:17:32Z")

</div>

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