insertReference( )


Inserts a reference in the specified property.

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.
Method Declaration
Copy
function insertReference(
  collectionId: string,
  propertyName: string,
  referringItem: union,
  referencedItem: union,
  options: WixDataOptions,
): Promise<void>;
Method Parameters
collectionIdstringRequired

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.


propertyNamestringRequired

The property to insert the reference into.


referringItemunionRequired

The referring item or referring item's ID.


referencedItemunionRequired

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


optionsWixDataOptions

An object containing options to use when processing this operation.

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.

JavaScript
import wixData from "wix-data"; // ... wixData .insertReference("movies", "actors", "00001", "12345") .then(() => { console.log("Reference inserted"); }) .catch((error) => { console.log(error); });
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?