Search.../

find( )

Returns the items that match the query.

Description

The find() function returns a Promise that resolves to the results found by the query and some information about the results. The Promise is rejected if find() is called with incorrect permissions or if any of the functions used to refine the query is invalid.

Calling the find() function triggers the beforeQuery() and afterQuery() hooks if they have been defined. If the query being run contains an include(), calling find() triggers beforeQuery() and afterQuery() hooks for the referenced collection as well.

Use the options parameter to override default preferences:

  • Override permission checks with suppressAuth.
  • Ensure the most up-to-date data is retrieved with consistentRead.
  • Prevent hooks from running with suppressHooks.
  • Speed up execution with omitTotalCount, if you don't need a count of items matching the query.

If you build a query and don't refine it with any wixDataQuery functions, find() returns the entire collection.

Syntax

function find([options: WixDataQueryOptions]): Promise<WixDataQueryResult>

find Parameters

NAME
TYPE
DESCRIPTION
options
Optional
WixDataQueryOptions

An object containing options to use when processing this operation.

Returns

Fulfilled - A Promise that resolves to the results of the query. Rejected - Error that caused the query to fail.

Return Type:

Was this helpful?

Perform a find on a query

This example demonstrates how to run a find() on a query() that was previously built and stored in a variable.

Copy Code
1query.find()
2 .then((results) => {
3 if(results.items.length > 0) {
4 let items = results.items;
5 let firstItem = items[0];
6 let totalCount = results.totalCount;
7 let pageSize = results.pageSize;
8 let currentPage = results.currentPage;
9 let totalPages = results.totalPages;
10 let hasNext = results.hasNext();
11 let hasPrev = results.hasPrev();
12 let length = results.length;
13 let query = results.query;
14 } else {
15 // handle case where no matching items found
16 }
17 })
18 .catch((error) => {
19 let errorMsg = error.message;
20 let code = error.code;
21 });
22
Create a query and run it

This example demonstrates how to build a query() and then run a find() on it.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.query("myCollection")
6 .find()
7 .then((results) => {
8 if(results.items.length > 0) {
9 let items = results.items;
10 let firstItem = items[0];
11 let totalCount = results.totalCount;
12 let pageSize = results.pageSize;
13 let currentPage = results.currentPage;
14 let totalPages = results.totalPages;
15 let hasNext = results.hasNext();
16 let hasPrev = results.hasPrev();
17 let length = results.length;
18 let query = results.query;
19 } else {
20 // handle case where no matching items found
21 }
22 })
23 .catch((error) => {
24 let errorMsg = error.message;
25 let code = error.code;
26 });
27
Create a query, add functions to the query, and run it

This example demonstrates how to build a query(), chain additional functions such as eq() and gt() to the query, and then run the query().

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.query("myCollection")
6 .eq("status", "active")
7 .gt("age", 25)
8 .ascending("last_name", "first_name")
9 .find()
10 .then((results) => {
11 if (results.items.length > 0) {
12 let items = results.items;
13 let firstItem = items[0];
14 let totalCount = results.totalCount;
15 let pageSize = results.pageSize;
16 let currentPage = results.currentPage;
17 let totalPages = results.totalPages;
18 let hasNext = results.hasNext();
19 let hasPrev = results.hasPrev();
20 let length = results.length;
21 let query = results.query;
22 } else {
23 // handle case where no matching items found
24 }
25 })
26 .catch((error) => {
27 let errorMsg = error.message;
28 let code = error.code;
29 });
30
Create a query with options and run it

This example demonstrates how to build a query() and then run a find() that bypasses permission checks and registered hooks. Note that the options parameter can only be used in backend code.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let options = {
6    "suppressAuth": true,
7    "suppressHooks": true
8};
9
10wixData.query("myCollection")
11 .find(options)
12 .then((results) => {
13 if(results.items.length > 0) {
14 let items = results.items;
15 let firstItem = items[0];
16 } else {
17 // handle case where no matching items found
18 }
19 })
20 .catch((error) => {
21 let errorMsg = error.message;
22 let code = error.code;
23 });
24