Looking for help with referencing object values

Hi, am very new to node-red and js .. getting the following from an mqtt input and trying to access the valuse in a js function to create an sql statment to post them into sqlite database with the same field names.

{"time":"2021-01-13 16:47:39","model":"SwitchDoc Labs FT020T AIO","device":12,"id":0,"batterylow":0,"avewindspeed":10,"gustwindspeed":20,"winddirection":272,"cumulativerain":27,"temperature":717,"humidity":97,"light":206,"uv":1,"mic":"CRC"}

Thank you for your time and trouble

Hi Ben.

When posting stuff like that, please use the </> button.

Which values to you want to access?

From where did you get this? A debug node?

(Edit or re-post the message and it may be easier to see too.)

I think I did use the </> .. but any way yes it is from a debug node on the output of my mqtt node.

Hi @BenMtl

this page in the docs explains how you can use the Debug sidebar to identify the path to any message element: Working with messages : Node-RED

It also helps you understand if msg.payload is a JSON String (where you can't directly access the individual properties inside it) or if it has already been parsed to a JavaScript Object where you can access its contents.

Worth a read through to see how that can help you understand what you've got.

Ok. Something didn't work then.

It should look a whole lot different that what is shown.

Try this:

To the left of the 1 key is the key which I can't show here. It is basically very similar to '
Press it 3 times. Press enter. Paste the stuff. Press enter. Press the afore mentioned key again 3 times.

Then it should be ok.

thanks I will check it again and see if I missed something.

I have corrected the post format. Somehow you had two lots of backticks round it, but also an extra single backtick at the start and end of the flow.

Thanks Colin

How are you getting on with the docs pointed to earlier by Nick?

going to sit down with it again in about 20 minutes. it shouldn't be that hard to pull the values from the key:value pairs to write to an db ..

Have a play with the Template node (not ui-template). That provides a good way of building a query string from a number of variables. I am assuming you know the query string you are trying to build.

yes very much so, just having an issue writing the js, to read the key values into the sql statment as values. was / am using the js function node .. had not thought about the template node.

It is often a good choice as it saves messing about with js. Feed the output into a debug node and get it working before connecting to the db.

oh yes .. always debug first. Almost got my js function working .. right now it is giving me the key names not the values in my out put .. grrr so damn close

Can I just check that you have a javascript object going into your function node and not a string?

yes it is an object .. it is just how I am calling the values back I am sure ..

Thanks all .. took me a bit to figure out how to referance the key values in js. Got it .. I am now gathering the data to an sqlite database :slight_smile:

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