# Select single value from collection

**URL:** <https://forum.wixstudio.com/t/select-single-value-from-collection/5033>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [November 3, 2021, 7:59pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033 "2021-11-03T19:59:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![widstje](https://avatars.discourse-cdn.com/v4/letter/w/cdc98d/32.png) [@widstje](https://forum.wixstudio.com/u/widstje)\
**Post date:** [November 3, 2021, 7:59pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033/1 "2021-11-03T19:59:00Z")

</div>

I’m trying to pull data into my webpage and push a result after the user types into a textbox and then clicks a button. It is basically a search bar and submit button.

Searching the field “Title” and I want to return the fourth field in the record. The result should also be a string.

I understand the query below is returning a record full of multiple fields each with their own value. I’d like to select an individual field within res.items and push that value forward. There should only be one record returned when the user queries the data.

Here’s my code. Any help would be appreciated.

import wixData **from** “wix-data” ;

export function button1\_click ( event ) {  
// Runs a query on the “GameData” collection  
wixData . query ( “GameData” )  
. contains ( “Title” , $w ( “#input1” ). value )  
. find () // Run the query  
. then ( res =\> {  
// Set textbox to the result of the query  
$w ( “#text1” ). text = res . items [3];  
});  
}

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [November 3, 2021, 8:20pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033/2 "2021-11-03T20:20:47Z")

</div>

You are using - - \> contains \< - - why?  
If you are searching for an exact matching and only one single item/value, why you do not use - → .eq(dbField, vlaue) then?

This should find an exact existing value (exact match) in your DATABASE.

Perhaps you also should show a little excerpt of your DB and an example of an end-result, you want to have after filtration/queriying.

---

<div class="post-metadata">

**Author:** ![widstje](https://avatars.discourse-cdn.com/v4/letter/w/cdc98d/32.png) [@widstje](https://forum.wixstudio.com/u/widstje)\
**Post date:** [November 3, 2021, 8:41pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033/3 "2021-11-03T20:41:49Z")

</div>

The result is “undefined” using .contains and I get the same using .eq. I know I have the collection and target field correct (checked case sensitivity).

Here’s my updated code:

import wixData from “wix-data” ;

export function button1\_click ( event ) {  
// Runs a query on the “GameData” collection  
wixData . query ( “GameData” )  
//.contains(“Title”, $w(“#input1”).value)  
. eq ( “Title” , $w ( “#input1” ). value )  
. find () // Run the query  
. then ( res =\> {  
// Set textbox to the result of the query  
//$w(“#text1”).text = items[3];  
console . log ( $w ( “#input1” ). value ); // This is working fine  
console . log ( res . items [3]);  
});  
}

I’m pretty new to Wix so I’m trying to figure out the best way to do things. I saw a tutorial (see below) that used .contains, which is why I used it.

> **[Velo Tutorial: Adding Collection Data Search Functionality | Help Center |...](https://support.wix.com/en/article/velo-tutorial-adding-collection-data-search-functionality)**
>
> Visit the Velo by Wix website to onboard and continue learning.This article assumes some understanding of database collections and datasets and how they are use

---

<div class="post-metadata">

**Author:** ![widstje](https://avatars.discourse-cdn.com/v4/letter/w/cdc98d/32.png) [@widstje](https://forum.wixstudio.com/u/widstje)\
**Post date:** [November 3, 2021, 9:02pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033/4 "2021-11-03T21:02:02Z")

</div>

Here’s a screenshot of the table.

 ![](https://us1.discourse-cdn.com/wix/original/3X/2/7/2764441191e59723daec3888b54c175593500832.png)

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [November 3, 2021, 9:23pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033/5 "2021-11-03T21:23:29Z")

</div>

Try this one…

```javascript
import wixData from "wix-data";

//-----------USER-INTERFACE----------------
const DATABASE = "DATABASE";
const DB_FIELD = "title";
//-----------USER-INTERFACE----------------

$w.onReady(()=>{$w('#button1').onClick(()=>{xxx();});})

function xxx() {
    wixData.query(DATABASE) 
    .eq(DB_FIELD, $w("#input1").value).find()  
    .then((res) => {console.log("Input-Value: ", $w("#input1").value); 
        let items = res.items; console.log(items);
        let firstItem = items[0]; console.log(firstItem);
        let title = firstItem.title; console.log(title);
        $w("#text1").text = title;
   }).catch((err)=>{console.log(err);});
}

```

Take a closer look onto - - \> CONSOLE! What do you get in CONSOLE?

Replace the values in the USER-INTERFACE-SECTION with your own ones…

- DATABASE  
-DB-FIELD

Have fun and good luck! (don’t forget to like it, if you really liked it 😉)

---

<div class="post-metadata">

**Author:** ![widstje](https://avatars.discourse-cdn.com/v4/letter/w/cdc98d/32.png) [@widstje](https://forum.wixstudio.com/u/widstje)\
**Post date:** [November 4, 2021, 5:46pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033/6 "2021-11-04T17:46:21Z")

</div>

Here are the results. Still not working.

 ![](https://us1.discourse-cdn.com/wix/original/3X/a/9/a99248672932a558c157064127eaa5d2198dc5b9.png)

---

<div class="post-metadata">

**Author:** ![widstje](https://avatars.discourse-cdn.com/v4/letter/w/cdc98d/32.png) [@widstje](https://forum.wixstudio.com/u/widstje)\
**Post date:** [November 4, 2021, 6:42pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033/7 "2021-11-04T18:42:24Z")

</div>

I went back and switched the field in the DB that I am trying to use in the query. I went from “Title” (type is text) to pFaction (type is number). I was able to get the code to work when I converted the input from $w(“#input1”) to a number using Number().

So this got me looking at the DB field Title, which is a string. When I was doing this I found an error in the Field Key, which for some reason was all lowercase. I think the software auto-populated this when I first setup the table. This difference in case ended up being the whole problem because I was trying to search the Field Name when it was looking at the Field Key.

 ![](https://us1.discourse-cdn.com/wix/original/3X/b/4/b4faabb30fdb22894a7ccb61fabc632dd6bab9e0.png)

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [November 4, 2021, 9:55pm UTC](https://forum.wixstudio.com/t/select-single-value-from-collection/5033/8 "2021-11-04T21:55:35Z")

</div>

All IDs of new generated DB-Fields gets a lower-case ID-Name (automaticaly).  
If you want to have it starting with a upper-case letter, than you have to set it up manualy on the first-time creation process.
