No output from function node?

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

[
    {
        "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
    }
]

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)

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) :

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;
        });
    }
}
  • 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
  • if you are doing async operations, you instead do node.send(msg) Reference

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

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

Thanks that was the solution !

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