TypeError: Cannot read property 'id' of undefined

I'm quite new to NodeRed. I'm trying to update a MySQL database using HTTP.


I'm using Postman as an HTTP client, sending a PUT request as follows:

The JSON outputted by the HTTP PUT request node is transformed to a Javascript Object by the JSON node. The INSERT DATA Function node takes the object and extracts the values of fields 'estado' and 'id', and uses them to construct a MySQL command in order to update the table. The code of the Function node is the following.

var obj = msg.payload[0];
var estado=obj["estado"];
var id =obj["id"];
msg.topic = "update biblioteca set estado='"+estado+"' where id="+id+";";
return msg;

However, when sending the request in Postman, I get the following error:

TypeError: Cannot read property 'id' of undefined

I'm sure the database and Postman are working properly, since the table is updated correctly when typing the values of "estado" and "id" manually in the funcion node code inesteado of extracting it from the object obj, so I must be doing something wrong when extracting the values of the fields.

However, the code worked properly for several hours after switching the JSON node property "Action" to "Always convert to javascript object". I had several blocks that worked well using similar code, the all of a sudden they all stopped working at once and outputting the same error, without me having changed their code or any other configuration.

Could it possibly be a bug in Node-Red? or am I doing something wrong?

Show us what you see in the debug node, screenshot if possible please.

Also, in the title you use the work "nombre" but in the text it is id, is there some confusion there?

image

Well that doesn't look much like what you were expecting. What does it look like if you put the debug node before the JSON node?

Your input data is “strange” the data is in the key (property) and the value is empty.

You can extract the key but first you should try to get the input correct.

var obj = msg.payload.keys()[0];

Should work if you do not get the input correct. But this is only a workaround- not recommended!

One of the issues will be that postman is configured to send a 'raw' payload. It ought to be sending application/json so the HTTP In node will parse it properly. You then won't need the JSON node in the flow.