wixData.insertReference() not working with single reference field

I’m doing a master child relationship for data entry. Basically one person can have many licenses.

The only way i could find to actually set the reference in the child record when creating that record, was to save it to the collection via its dataset and then use the insertReference function https://www.wix.com/code/reference/wix-data.html#insertReferencefunction to insert the _id of the record that i wanted associated as the parent…however…
I have discovered that running this function only works on multi- reference fields (many-many style relationship). If i run the function against a single reference field it errors out.

Is this a required item or a bug?

I did notice that you can pass an array of items into that field, but I believe it should only enforce the “multi-reference” field rule if an array is being passed (multiple references to that child record)

The issue is on the lightbox DriversLicense
The code in question is in the function buttonSaveClose_click

here’s the editor link for the site https://editor.wix.com/html/editor/web/renderer/edit/a4e231dd-d07a-49d5-ac5f-3b2d2f821df1?metaSiteId=7a12991b-6802-4baa-bc84-3fd4c51da5e5&editorSessionId=fd80c2be-648c-4de1-b36f-2e0d3a51329e&referralInfo=my-account

…still very much a work in progress and my first site ever so be gentle!! lol

David, if you want to do 1-n (instead of n-m), wixData.insert should be enough. When you write the n-part, just make sure you fill the reference field with the _id of the main record.

1 Like

facepalm… yeah i think that might do the trick… I guess I “nuked” it… overthinking the problem :slight_smile: I’ll let you know how it works out here when I have a few minutes to implement and test.

Tested and success!

export function buttonSaveClose_click(event, $w) {
//run validation
if (!validateFields()) {
return ;

// get the data from the dataset current item (easier to do than creating and tracking an object myself)

let currentItemObj = $w(‘#datasetDriversLicenseInfo’).getCurrentItem();

//add the referenced item by adding a new field to the copied object and providing the reference value

currentItemObj.driverReference = wixWindow.lightbox.getContext(); 

//MANUALLY save the data (not using the dataset but using wixData)

wixData.insert('DriverLicenseInformation', currentItemObj) 
    .then(() => console.log('save succeeded'), ) 

}// tested and works so far!
//TODO: local error handling
//TODO: notification to calling function so data refreshes can happen if need be. use the lightbox close passing mechanism
//TODO: udate layouts for cleaner workflow and more advanced validations

1 Like

@recruitingdepartment glad you got it working.

1 Like

I do think the docs should be changed to reflect this…