# connecting a database

**URL:** <https://forum.wixstudio.com/t/connecting-a-database/40895>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [June 5, 2021, 9:12pm UTC](https://forum.wixstudio.com/t/connecting-a-database/40895 "2021-06-05T21:12:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danebertbalzano](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@danebertbalzano](https://forum.wixstudio.com/u/danebertbalzano)\
**Post date:** [June 5, 2021, 9:12pm UTC](https://forum.wixstudio.com/t/connecting-a-database/40895/1 "2021-06-05T21:12:28Z")

</div>

Would anybody be able to help me determine what issue I am having with my code for connecting a search button to a database? Below is my code.

The search button appears to have no functionality. My table displays all results from the collection/dataset instead of refining results. The page contains a user input, a button, and a table.

I copied and pasted from a VELO tutorial. I have limited / no coding experience except for what’s posted below.

**import** wixData **from** “wix-data” ;  
export function searchButton\_click ( event )  
{ wixData . query ( “VaultSearch” )  
. contains ( “title” , $w ( “#search” ). value )  
. find ()  
. then ( res =\> { $w ( “#results” ). rows = res . items ;  
});}  
$w . onReady ( **function** () {  
$w ( “#results” ). columns = [  
{  
“id” : “col1” ,  
“dataPath” : “title” ,  
“label” : “Name” ,  
“width” : 100 ,  
“type” : “string” ,  
},  
{ “id” : “col2” ,  
“dataPath” : “url” ,  
“label” : “URL” ,  
“width” : 100 ,  
“type” : “string” ,},  
{ “id” : “col3” ,  
“dataPath” : “vault” ,  
“label” : “Vault” ,  
“width” : 100 ,  
“type” : “string” ,},  
}];  
});

---

<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:** [June 5, 2021, 11:33pm UTC](https://forum.wixstudio.com/t/connecting-a-database/40895/2 "2021-06-05T23:33:24Z")

</div>

You will need to do it some kind of this way…

```javascript
import wixData from 'wix-data';
import wixUsers from 'wix-users';

$w.onReady(async()=>{ 
  $w("#myTableIDhere").columns = [//TABLE-SET-UP is CORRECT ?
    {"id":"col1","dataPath":"title","label":"Name","width":100,"type":"string"},
    {"id":"col2","dataPath":"url","label":"URL","width":100,"type":"string"},
    {"id":"col3","dataPath":"vault","label":"Vault","width":100,"type":"string"}
  ];
   
  $w('#searchButton').onClick(()=>{
     myFunction();
  });
});

function myFunction(){
  let DATABASE = "VaultSearch"
  let DATAFIELD = "title"
  let VALUE = $w("#search").value
 
  wixData.query(DATABASE)
 .contains(DATAFIELD,VALUE)
 .find()
 .then((results) => {
   if(results.items.length > 0) {console.log("ITEMS: "; results.items)
     let firstItem = results.items[0]; console.log("FIRST-ITEM: ", firstItem)
     let secondItem = results.items[1]; console.log("SECOND-ITEM: ", secondItem)
     let thirdItem = results.items[2]; console.log("THIRDT-ITEM: ", thirdItem)
     let title = results.items[0].title; console.log("TITLE: ", title)
     
     $w("#myTableIDhere").rows = res.items;
 
   //What to do next???????
   //Continue here, now it''s your turn....
   //....your code 
   //...countinue
   //..here
   //.
   //
 
   } else { }
 })
 .catch((err) => {let errorMsg = err;});
}
 
 
//OUTPUT-EXAMPLE:
//------------------- 
/* firstItem is:
 *
 * {
 * "_id": "00001",
 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
 * "_createdDate": "2017-05-24T12:33:18.938Z",
 * "_updatedDate": "2017-05-24T12:33:18.938Z",
 * "title": "Mr.",
 * "first_name": "John",
 * "last_name": "Doe"
 * }
 */

```

Not tested. You will perhaps have to optimize and modify it a little bit.

Good luck!
