I have a simple form that takes the one or more terms entered into a text field (input2),splits the terms, and then searches for those terms in my collection called "teaching files". I know it splits the terms fine as the console log shows the array of terms but I keep getting "table result is undefined". I presume that means the query did not work. Does anyone have any insights? Thank you!
import wixData from 'wix-data';
export function button1_click(event, $w) {
let searchValue = $w('#input2').value;
let searchWords = searchValue.split(' ');
console.log(searchWords);
let query = wixData.query('teachingfiles')
.hasSome('comments', searchWords);
query.find().then(res =>
{
$w('#table2').rows = res.items;
let table_result= $w('#table2');
console.log("table result is " + table_result.items);
})
.catch(err =>
{
console.log("problem in search! " + err);
});
}
$w.onReady(
function () {
$w("#table2").columns = [
{
"id": "diagnosis", // ID of the column for code purposes
// The field key in the collection whose data this column displays
"dataPath": "diagnosis",
"label": "Diagnosis", // The column header
"width": 10, // Column width
"visible": true, // Column visibility
"type": "string", // Data type for the column
// Path for the column if it contains a link
"linkPath": "link-field-or-property"
},
{
"id": "comment",
"dataPath": "comment",
"label": "Details",
"width": 900,
"visible": true,
"type": "string",
"linkPath": "link-field-or-property"
}
];
});
@mnigogos your field key should be "comment", although you wrote in the code "comments".
Second thing, "hasSome" API is used to find keywords exactly, it will not match one word inside a sentence. For that, you need to use the contains API. If you want to search by multiple keywords, you can call "contains" multiple times, each time with a different keyword, something like this:
let searchWords = searchValue.split(' '); let query = wixData.query('teachingfiles') searchWords.forEach(s => query = query.contains('comment', s))
Can you please log "res.items" directly and see what you get?