Error after deploying a project

Hi,

I get the error shown in the image only once and at start of the project.
What could cause it?
TIA

Configure the node to send a string rather than parsed json and see what the first message received is.

Thanks, that did it - . I had to modify my function node to parse the json data.

var msgSensor1 = { payload: 0 };
var msgSensor2 = { payload: 0 };
var msgPulse = { payload: 0 };
var msgTemp = { payload: 0 };
var msgPump = { payload: 0 };
var msgEmail = { payload: 0 }
var msgVer = { payload: 0 };

if (msg.topic !== 'nada') {
    var string = JSON.stringify(msg.payload);
    //var p = JSON.parse(string); <-- commented out
    var p = JSON.parse(msg.payload);
    msgSensor1 = { payload: parseInt(p.s1) };
    msgSensor2 = { payload: parseInt(p.s2) };
    msgPulse = { payload: p.hb };
    msgTemp = { payload: p.t };
    msgEmail = { payload: p.em };
    msgPump = { payload: p.pn };
    msgVer = { payload: p.ver };

    if (msgEmail.payload == 1 && msgPump.payload == 1){
        msgEmail.payload = 1;
    }
    else if (msgEmail.payload == 1 && msgPump.payload == 2){
        msgEmail.payload = 2;
    }
    else {
        msgEmail.payload = 0;
    }
    
    return [msgSensor1, msgSensor2, msgPulse, msgTemp, msgEmail, msgVer];
}

That was not supposed to be a solution, what you have done is probably not the best way to solve it. What is in the first message coming in (the one that caused the json error) now that you have stopped the node from trying to parse it? Feed it into a debug node to see. If necessary, set the debug node to output to the console then run node red from a command window and you will see the debug output there.

Ok, any improvements for my general knowledge, is welcome :wink:
This' what I get from the mqtt receiver node:

"{"s1": 100, "hb": 0, "pn": 0, "s2": 100, "ver": "8", "em": 0, "t": 25.0}"

The data comes from a Rpi Pico W micro running with micropython.

Now, if I set the mqtt receiver node back to "a parsed JSON object", I get this format:

11/20/2023, 9:25:58 AM [node: Parse Json] 
(http://192.168.1.230:1880/#)
function : (error)
"SyntaxError: Unexpected token o in JSON at position 1"
11/20/2023, 9:25:58 AM [node: debug 1]
(http://192.168.1.230:1880/#)casa/rp2/soil/moist : msg.payload : Object
object
s1: 100
hb: 0
pn: 0
s2: 100
ver: "8"
em: 0
t: 27

and, if I set the node to "auto detect string or buffer", I get this:

11/20/2023, 9:30:26 AM node: debug 1
casa/rp2/soil/moist : msg.payload : string[72]
"{"s1": 100, "hb": 1, "pn": 0, "s2": 100, "ver": "8", "em": 0, "t": 28.0}"

Which MQTT node are you using (full name please i.e. node-red-???????) You can go to the top right of the editor and click the 'hamburger' icon and select Manage palette then enter mqtt in the search.

You get an object.

If you then pass that into your function node, JSON.parse(msg.payload); will throw an error

Proof:


When you have a string your function node will call var p = JSON.parse(msg.payload); and convert the string to an object

Proof:

Also worthy of note: s1 and s2 are already numbers - calling parseInt(p.s1) is redundant.

Proof:

I thought you meant that the error was coming from the MQTT node, whereas it is actually coming from the function node.

With the MQTT node set to parse the json it is receiving and output an object, it will output an object, so you don't need to parse it in the function node. You should be able to take out the stringify and the parse in the function node.

I guess the one it comes with the standard package as it doesn't show any names. I has two options: mqtt in and mqtt out.

I see what you mean. In my first post the img shows where the error was generated that's why I thought it came from the mqtt node.

That's one of my pending tasks: learn how to use a cmd console and not just rely on the web console. Thanks!

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