get( )
Retrieves an item from a collection.
Description
The get()
function returns a Promise that resolves to the item retrieved from the specified
collection, or null if the item is not found. The Promise is rejected if the
current user does not have read permissions for the collection.
If the specified collection contains reference fields, the ID of the referenced item is returned. To return the values of the referenced items use query()
and include()
.
Calling the get()
function triggers the beforeGet()
and afterGet()
hooks if they have been defined.
Use the options
parameter to run get()
from backend code without
checking for permissions or without its registered hooks.
Notes:
- To find the
itemId
, usequery()
. You can also find it under ID in the CMS system fields.- When using the
query()
orget()
functions or another data retrieval method following a change to your database collection, the data retrieved may not contain your most recent changes. See Wix-data and Eventual Consistency for more information. To solve this problem, you can use thesetTimeout()
function to delay retrieving data following any changes to your database collection.- An
itemId
is required to retrieve an item even from a single-item collection.
Syntax
function get(collectionId: string, itemId: string, [options: WixDataOptions]): Promise<Object>
get Parameters
NAME
TYPE
DESCRIPTION
The ID of the collection to retrieve the item from.
To find your collectionId
, select the Databases tab in the Velo Sidebar.
Hover over your collection, click the three dots, and select Edit Settings.
The ID of the item to retrieve.
An object containing options to use when processing this operation.
Returns
Fulfilled - The retrieved item or null if not found. Rejected - The error that caused the rejection.
Return Type:
Was this helpful?
1import wixData from 'wix-data';23// ...45wixData.get("myCollection", "00001")6 .then((item) => {7 console.log(item); //see item below8 })9 .catch((err) => {10 console.log(err);11 });1213/* item is:14 *15 * {16 * "_id": "00001",17 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",18 * "_createdDate": "2017-05-24T12:33:18.938Z",19 * "_updatedDate": "2017-05-24T12:33:18.938Z",20 * "title": "Mr.",21 * "first_name": "John",22 * "last_name": "Doe"23 * }24 */
1import wixData from 'wix-data';23// ...45let options = {6 "suppressAuth": true,7 "suppressHooks": true8};910wixData.get("myCollection", "00001", options)11 .then((item) => {12 console.log(item); //see item below13 })14 .catch((err) => {15 console.log(err);16 });1718/* item is:19 *20 * {21 * "_id": "00001",22 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",23 * "_createdDate": "2017-05-24T12:33:18.938Z",24 * "_updatedDate": "2017-05-24T12:33:18.938Z",25 * "title": "Mr.",26 * "first_name": "John",27 * "last_name": "Doe"28 * }29 */