# Select array-element from a database with the "form" node

**URL:** <https://discourse.nodered.org/t/select-array-element-from-a-database-with-the-form-node/3610>\
**Category:** Dashboard\
**Created:** [2 October 2018 08:12 UTC](https://discourse.nodered.org/t/select-array-element-from-a-database-with-the-form-node/3610 "2018-10-02T08:12:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimSchmidt](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@TimSchmidt](https://discourse.nodered.org/u/TimSchmidt)\
**Post date:** [2 October 2018 08:12 UTC](https://discourse.nodered.org/t/select-array-element-from-a-database-with-the-form-node/3610/1 "2018-10-02T08:12:11Z")

</div>

Hi together,  
I built the folowing flow:

 ![aktuellerStandmarkiert](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/37a2b2ff684ab9ede6205b04d8f7c87ef7b3b4b1.jpeg)

I use it to get data from a database. I have to choose the element of the array manually.  
In my example it is element 1.

This is the corresponding code:

Node "Datenabfrage\_FROM\_orders:  
msg.topic = "SELECT \* FROM orders";  
return msg;

Node "Abfrage Auftragsinformationen":  
//choose an element from the array of the database  
var o\_id = 1;

var number\_items = msg.payload[o\_id].number\_items;  
var number\_finished = msg.payload[o\_id].number\_finished;

var itemstatus = {};  
itemstatus.labels = ["Herzustellen", "Fertiggestellt"];  
itemstatus.data = [[number\_items, number\_finished]];  
itemstatus.series = ["Sales"];

var prozent\_finished = parseInt([(number\_finished / number\_items)\*100])

var test = itemstatus.data[0][1];

return [{payload:[itemstatus],topic:msg.topic},  
{payload: prozent\_finished},  
{payload: test},  
{payload: o\_id}  
];

This works and the data are shown on the dashboard.

But now I want to choose an element from the array by tiping in a number on the dashboard and not changing the function node "Abfrage Auftragsinformationen" all the time manually. My plan is to use the "form" node.

I have tried a lot of ways to realise this plan, but I have only little experience.  
There are always new errors (e.g. syntax errors) and I have absolutly no idea how to realise my idea.

Can somebody help me, please.

If it is not understandable what I wanted to explain or if you need more information, please ask.

Tank you in advance

(Sorry, as a new member I'm just allowed to post 1 image)

---

<div class="post-metadata">

**Author:** ![TimSchmidt](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@TimSchmidt](https://discourse.nodered.org/u/TimSchmidt)\
**Post date:** [2 October 2018 08:13 UTC](https://discourse.nodered.org/t/select-array-element-from-a-database-with-the-form-node/3610/2 "2018-10-02T08:13:22Z")

</div>

Supplement:

This is the array from the database

 ![Datenbankausgabemarkiert](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/76a6d50751e89308d30f84ef34d0d7348c3e0769.jpeg)

---

<div class="post-metadata">

**Author:** ![TimSchmidt](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@TimSchmidt](https://discourse.nodered.org/u/TimSchmidt)\
**Post date:** [2 October 2018 08:14 UTC](https://discourse.nodered.org/t/select-array-element-from-a-database-with-the-form-node/3610/3 "2018-10-02T08:14:05Z")

</div>

Supplement 2:

And this is what I want to do

 ![Zielzustandmarkiert](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e538cecfc6f6d4e382365b3423872b30ca345dd4.jpeg)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [2 October 2018 14:55 UTC](https://discourse.nodered.org/t/select-array-element-from-a-database-with-the-form-node/3610/4 "2018-10-02T14:55:54Z")

</div>

It sounds like you want some user input from the dashboard to filter the records returned by your Sql database query. So for instance, if there was a dropdown of the o\_id values, selecting one of them could send that value in the `msg.payload` to a node that _includes_ that value in your query statement -- so the `msg.topic` sent to the Sql node would be in this format:

`SELECT * FROM orders WHERE o_id = 1;`

There are several ways to take the UI form output and build the query string (e.g. using a function, template, or change node). This is more efficient than returning all the database rows and selecting the desired row by index number.

---

<div class="post-metadata">

**Author:** ![TimSchmidt](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@TimSchmidt](https://discourse.nodered.org/u/TimSchmidt)\
**Post date:** [6 October 2018 08:25 UTC](https://discourse.nodered.org/t/select-array-element-from-a-database-with-the-form-node/3610/5 "2018-10-06T08:25:39Z")

</div>

First thanks a lot for your reply. It helped me solving my problem.

When everything works as designated I will try out some other (more efficient) ways to take the UI form as you suggestet.

BR Tim
