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"
}
];
});
Can you please log “res.items” directly and see what you get?
I ran two logs in the following location in the code:
query.find().then(res =>
{
$w(’ #table2 ‘).rows = res.items;
let table_result= $w(’ #table2 ');
console.log(“The items are "” + res.items +“"”);
console.log("Table result is " + table_result.items);
})
I get the following response:
The items are “”
Table result is undefined
Since posting I see other mistakes (newbie) but in the interest of reducing confusion, I’ve changed nothing but console.log.
@mnigogos Can you please share your site? If this is not live yet, you can post your editor URL. I’ll try to take a look.
@mnigogos your field key should be “comment”, although you wrote in the code "comment s ".
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))
@tomer-wix Thank you so much. I added your code and now want to SEE what the results are (or simply dump it into my table. I presume “query” is now an object that contains the rows I can subsequently address (or put in a table). Is there a way to simply show the results-obviously “query.items” is wrong!
let searchValue = $w(‘#input2’).value
let searchWords = searchValue.split(’ ');
let query = wixData.query(‘teachingfiles’);
searchWords.forEach(s => query = query.contains(‘comment’, s))
console.log("Query resut is " + query.items);
Odd. Site reverted to original state. Corrected.