find( )
Returns the documents that match the search.
Description
The find()
function returns a Promise that resolves to the results found
by the search and some information about the results. The Promise is
rejected if any of the functions used to refine the search are invalid.
If you build a search and don't refine it with any WixSearchBuilder
functions,
find()
returns all matching site documents.
Syntax
function find(): Promise<WixSearchResult>
find Parameters
This function does not take any parameters.
Returns
Fulfilled - A Promise that resolves to the results of the search. Rejected - Error that caused the search to fail.
Return Type:
Was this helpful?
This example demonstrates how to run a find()
on a search()
that was previously built and stored in a variable.
1search.find()2 .then((results) => {3 if (results.documents.length > 0) {4 let documents = results.documents;5 } else {6 console.log("No matching results");7 }8 })9 .catch((error) => {10 console.log(error);11 });12
This example demonstrates how to build a search()
and then run a find()
on it.
1import wixSearch from 'wix-search';23// ...45$w("#searchInput").onKeyPress((keyPress) => {6 if (keyPress.key === "Enter") {7 const phrase = $w("#searchInput").value;8 wixSearch.search(phrase)9 .find()10 .then((results) => {11 if (results.documents.length > 0) {12 let documents = results.documents;13 } else {14 console.log("No matching results");15 }16 })17 .catch((error) => {18 console.log(error);19 });20 }21});22
This example demonstrates how to build a search, chain additional functions such as eq()
and limit()
, and then run the search.
1import wixSearch from 'wix-search';23// ...45$w("#searchInput").onKeyPress((keyPress) => {6 if (keyPress.key === "Enter") {7 const phrase = $w("#searchInput").value;8 wixSearch.search(phrase)9 .documentType("Stores/Products")10 .eq("onSale", true)11 .ascending("sku")12 .limit(5)13 .find()14 .then((results) => {15 if (results.documents.length > 0) {16 let documents = results.documents;17 } else {18 console.log("No matching results");19 }20 })21 .catch((error) => {22 console.log(error);23 });24 }25});26
This example demonstrates how to set up a search for store products and display the results in a repeater. Each button in the repeater links to the product page for the selected product.
1import wixSearch from 'wix-search';23// ...45// Before search runs, set the repeater data as empty6$w('#repeater').data = [];78$w("#searchInput").onKeyPress((keyPress) => {9 if (keyPress.key === "Enter") {10 const phrase = $w("#searchInput").value;11 wixSearch.search(phrase)12 .documentType("Stores/Products")13 .find()14 .then((results) => {15 if (results.documents.length > 0) {16 $w('#repeater').data = results.documents;17 } else {18 console.log("No matching results");19 }20 })21 .catch((error) => {22 console.log(error);23 });24 }25});2627$w("#repeater").onItemReady(($item, itemData) => {28 $item("#title").text = itemData.title;29 $item("#description").text = itemData.description;30 $item("#image").src = itemData.image;31 $item("#button").link = itemData.url;32});33