I am new user of Node red and I am working on a project to show random question on dashboard.
The user answers by pushing on one of the presented button (True / False) presented on dashborad.
When simulating the flow, I have an error : "TypeError: Cannot read properties of undefined (reading 'reponse )".
Could you help me to find the problem.
Thank you in advance.
Here you will find the code.
[
{
"id": "343e552e8fc04ec0",
"type": "tab",
"label": "Flow 4",
"disabled": false,
"info": "",
"env": [ ]
},
{
"id": "78234bc16c766e6a",
"type": "inject",
"z": "343e552e8fc04ec0",
"name": "Départ quiz",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 230,
"y": 120,
"wires": [
[
"a177a810816289f2"
]
]
},
{
"id": "a177a810816289f2",
"type": "function",
"z": "343e552e8fc04ec0",
"name": "Générer question",
"func": "const questions = [\n {\n question: \"La première révolution industrielle a introduit l'utilisation de la vapeur et des machines à vapeur. Vrai ou faux ?\",\n reponse: true\n },\n {\n question: \"La deuxième révolution industrielle était caractérisée par l'utilisation de l'électricité et des chaînes de montage. Vrai ou faux ?\",\n reponse: true\n },\n {\n question: \"La troisième révolution industrielle est souvent appelée \\\"révolution numérique\\\" ou \\\"révolution informatique\\\". Vrai ou faux ?\",\n reponse: true\n },\n {\n question: \"La quatrième révolution industrielle est caractérisée par l'intégration de technologies numériques, biologiques et physiques. Vrai ou faux ?\",\n reponse: true\n }\n];\n\n// Sélectionner aléatoirement une question\nconst questionAleatoire = questions[Math.floor(Math.random() * questions.length)];\n\n// Envoyer la question\nmsg.question = questionAleatoire;\nmsg.payload = questionAleatoire.question;\n//msg.payload = msg.question.reponse;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 510,
"y": 120,
"wires": [
[
"51c4338cc5116254",
"eecbd22fdf0c1027"
]
]
},
{
"id": "51c4338cc5116254",
"type": "ui_text",
"z": "343e552e8fc04ec0",
"group": "c7152d4603350716",
"order": 1,
"width": 18,
"height": 2,
"name": "",
"label": "Question",
"format": "{{msg.payload}}",
"layout": "col-center",
"className": "",
"style": "",
"font": "",
"fontSize": "",
"color": "#000000",
"x": 800,
"y": 120,
"wires": []
},
{
"id": "675199f3b64efb40",
"type": "ui_button",
"z": "343e552e8fc04ec0",
"name": "Vrai",
"group": "c7152d4603350716",
"order": 4,
"width": 3,
"height": 1,
"passthru": false,
"label": "Vrai",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "true",
"payloadType": "bool",
"topic": "",
"topicType": "str",
"x": 150,
"y": 240,
"wires": [
[
"1e851fe0b5cfb5df"
]
]
},
{
"id": "14504d4c6029deef",
"type": "function",
"z": "343e552e8fc04ec0",
"name": "Vérifier réponse",
"func": "const reponseUtilisateur = msg.payload;\nconst reponseAttendue = msg.question.reponse;\n\nif (reponseUtilisateur === reponseAttendue) {\n msg.payload = \"Correct !\";\n} else {\n msg.payload = \"Incorrect.\";\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 560,
"y": 300,
"wires": [
[
"33139767b681cd82",
"3978b4a960fdb2ab"
]
]
},
{
"id": "33139767b681cd82",
"type": "ui_text",
"z": "343e552e8fc04ec0",
"group": "c7152d4603350716",
"order": 10,
"width": 6,
"height": 1,
"name": "Résultat",
"label": "",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"style": "",
"x": 760,
"y": 220,
"wires": []
},
{
"id": "937eeb5907498e47",
"type": "ui_button",
"z": "343e552e8fc04ec0",
"name": "Faux",
"group": "c7152d4603350716",
"order": 6,
"width": 3,
"height": 1,
"passthru": false,
"label": "Faux",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "false",
"payloadType": "bool",
"topic": "",
"topicType": "str",
"x": 150,
"y": 300,
"wires": [
[
"1e851fe0b5cfb5df"
]
]
},
{
"id": "3978b4a960fdb2ab",
"type": "debug",
"z": "343e552e8fc04ec0",
"name": "Réponse utilisateur",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 830,
"y": 360,
"wires": []
},
{
"id": "eecbd22fdf0c1027",
"type": "debug",
"z": "343e552e8fc04ec0",
"name": "debug 1",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 700,
"y": 40,
"wires": []
},
{
"id": "1e851fe0b5cfb5df",
"type": "function",
"z": "343e552e8fc04ec0",
"name": "function 1",
"func": "// Vérifier si msg.question existe et possède la propriété 'reponse'\n\n\nif (msg.question && typeof msg.question === 'object' && msg.question.hasOwnProperty('reponse')) {\n // Votre logique pour vérifier la réponse\n const reponseUtilisateur = msg.payload;\n const reponseAttendue = msg.question.reponse;\n \n\n if (reponseUtilisateur === reponseAttendue) {\n msg.payload = \"Correct !\";\n } else {\n msg.payload = \"Incorrect.\";\n }\n} else {\n // Cas où msg.question est indéfini ou ne possède pas la propriété 'reponse'\n msg.payload = \"Erreur : Données de question manquantes.\";\n const reponseUtilisateur = msg.payload;\n msg.payload = \"valeur utilisateur\";\n msg.payload = reponseUtilisateur;\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 340,
"y": 260,
"wires": [
[
"14504d4c6029deef"
]
]
},
{
"id": "6e3306b717982d13",
"type": "ui_spacer",
"z": "343e552e8fc04ec0",
"name": "spacer",
"group": "c7152d4603350716",
"order": 2,
"width": 18,
"height": 1
},
{
"id": "d31d6539a9186b54",
"type": "ui_spacer",
"z": "343e552e8fc04ec0",
"name": "spacer",
"group": "c7152d4603350716",
"order": 3,
"width": 1,
"height": 1
},
{
"id": "4caccfd7166af9c1",
"type": "ui_spacer",
"z": "343e552e8fc04ec0",
"name": "spacer",
"group": "c7152d4603350716",
"order": 5,
"width": 10,
"height": 1
},
{
"id": "3f35559fb7f6ea2c",
"type": "ui_spacer",
"z": "343e552e8fc04ec0",
"name": "spacer",
"group": "c7152d4603350716",
"order": 7,
"width": 1,
"height": 1
},
{
"id": "f298411fa2a5f24d",
"type": "ui_spacer",
"z": "343e552e8fc04ec0",
"name": "spacer",
"group": "c7152d4603350716",
"order": 8,
"width": 18,
"height": 1
},
{
"id": "a925aeb749633fbd",
"type": "ui_spacer",
"z": "343e552e8fc04ec0",
"name": "spacer",
"group": "c7152d4603350716",
"order": 9,
"width": 7,
"height": 1
},
{
"id": "d76a746cb151199e",
"type": "ui_spacer",
"z": "343e552e8fc04ec0",
"name": "spacer",
"group": "c7152d4603350716",
"order": 11,
"width": 5,
"height": 1
},
{
"id": "c7152d4603350716",
"type": "ui_group",
"name": "Default",
"tab": "95e6a5b444895807",
"order": 1,
"disp": true,
"width": 18,
"collapse": false,
"className": ""
},
{
"id": "95e6a5b444895807",
"type": "ui_tab",
"name": "Chef d'oeuvre",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]