insertReference( )
Inserts a reference in the specified property.
Description
The insertReference()
function returns a Promise that resolves when a
reference to the referenced item(s) is added to the referring item in the
specified property. The Promise is rejected if the current user does not
have update permissions for the collection.
Calling the insertReference()
function does not trigger any hooks.
Notes:
- The
insertReference()
function only applies to multi-reference fields.- The
insertReference()
function is not supported for Single Item Collections.
Syntax
function insertReference(collectionId: string, propertyName: string, referringItem: Object | string, referencedItem: Object | string | Array<Object> | Array<string>, [options: WixDataOptions]): Promise<void>
insertReference Parameters
NAME
TYPE
DESCRIPTION
The ID of the collection that contains the referring item.
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 property to insert the reference into.
The referring item or referring item's ID.
The referenced item, referenced item's ID, an array of referenced items, or an array of referenced item IDs.
An object containing options to use when processing this operation.
Returns
Fulfilled - When the references have been inserted. Rejected - The error that caused the rejection.
Return Type:
Was this helpful?
This example inserts a reference to the item with ID 12345 in the Actors field of the item in the Movies collection with the ID 00001.
1import wixData from 'wix-data';23// ...45wixData.insertReference("movies", "actors", "00001", "12345")6 .then(() => {7 console.log("Reference inserted");8 })9 .catch((error) => {10 console.log(error);11 });12
This example inserts a reference to the items with IDs 12345 and 67890 in the Actors field of the item in the Movies collection with the ID 00001.
1import wixData from 'wix-data';23// ...45wixData.insertReference("movies", "actors", "00001", ["12345", "67890"])6 .then(() => {7 console.log("Reference inserted");8 })9 .catch((error) => {10 console.log(error);11 });12