Search.../

fields( )

Lists the fields to return in a query's results.

Description

The fields() function returns only the specified fields in the query's results.

You can use include() in conjunction with fields() to get referenced items.

When fields() receives an empty or invalid property, the query behaves as follows:

  • When no fields are specified, the query returns all fields.
  • When multiple fields are specified but some are invalid, invalid fields are ignored and valid fields are returned.
  • When only invalid fields are specified, only the default _id field is returned.

Syntax

function fields(propertyName: ...string): WixDataQuery

fields Parameters

NAME
TYPE
DESCRIPTION
propertyName
string

Properties to return. To return multiple properties, pass properties as additional arguments.

Returns

A WixDataQuery object representing the query.

Return Type:

Was this helpful?

Add a fields function to a query

Copy Code
1let myQuery = query.fields("fieldName1", "fieldName2");
Create a query, specify fields to return, and run it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.query("books")
6 .fields("authorLastName", "title")
7 .find()
8 .then((results) => {
9 let books = results.items;
10 } else {
11 // handle case where no matching items found
12 }
13 )
14 .catch((error) => {
15 let errorMsg = error.message;
16 let code = error.code;
17 });
18/* Items returned contain the _id field in addition to the requested fields.
19 * [
20 * {
21 * _id: '123',
22 * authorLastName: 'Rowling',
23 * title: 'Fantastic Beasts'
24 * },
25 * {
26 * _id: '456',
27 * authorLastName: 'Melville',
28 * title: 'Moby Dick'
29 * }
30 * ]
31 */