Search.../

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
collectionId
string

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.

propertyName
string

The property to insert the reference into.

referringItem
Object | string

The referring item or referring item's ID.

referencedItem
Object | string | Array<Object> | Array<string>

The referenced item, referenced item's ID, an array of referenced items, or an array of referenced item IDs.

options
Optional
WixDataOptions

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:

Promise<void>

Was this helpful?

Insert a reference

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.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.insertReference("movies", "actors", "00001", "12345")
6 .then(() => {
7 console.log("Reference inserted");
8 })
9 .catch((error) => {
10 console.log(error);
11 });
12
Insert references using an array

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.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.insertReference("movies", "actors", "00001", ["12345", "67890"])
6 .then(() => {
7 console.log("Reference inserted");
8 })
9 .catch((error) => {
10 console.log(error);
11 });
12