How to insert item with multi-reference field

Hi

I need to create an event with multiple attendee via code . I got a collection “events” (date,title, attendees) with a multi-references field called “attendees” which refers to a collection of attendee (name, events).

What is the proper way to insert/save a new item in that collection?

I was hoping to be able to do something like that

import wixData from 'wix-data';

let attendees = [attendee1._id, attendee2._id, attendee3._id] ;

let newEvent = {
  "date":  new Date(),
  "title":   "my event title",
  "attendees": attendees
};

wixData.insert("events", newEvent)
  .then(...)
  1. is that the correct way? or is there a better way to do it?

  2. should I also update the attendee collection field “events” to keep it sync?
    thx you

2 Likes

Hi Quentin… did you ever receive any feedback on this? I have the same question and am looking for answers how to best utilize the multi-reference fields using the form elements and dynamic pages.

I ended up creating a joint collection (eventID, attendeeID) and do a bulk insert of attendees Id referencing 1 event. That suited better my use case and it bypass some limitation of multi referencement (like maximum 50 refs or something like that).

let me know if you need more help :slight_smile:

Another solution to this problem:

let headingArray = [];

export function buttonSubmit_click(event) {
headingArray.push(headingID)
let dataObj = {
"title": $w("#input1").value,
"shortDescription": $w("#textBox1").value
}
wixData.insert("Listings", dataObj)
.then((res) => {
let itemID = res._id;
wixData.insertReference("Listings", "heading", itemID, headingArray)
})
}

So this approach is quite straightforward. First, I create an array called headingArray, then I add some items to it via .push

Then I proceed to create an item in the database excluding the array items. When that item has been created, I get the _id from the result and simply update that item with the array data by using insertReference

As I said, quite straightforward and works surprisingly fast.

Good luck!
Tiaan

Hi Tiaan,
Can You help me to answer what is “heading” and “Listings” in you straightforward approach, please!? I have to solve to insert member’s input into the reference field of my database called “top10jeloltek”, please!!!
Thanks very much in advance!
Laszlo

@dalanyi https://www.wix.com/corvid/reference/wix-data/insertreference

those are the two databases

with regards to yours, top10jeloltek is one, and referenced with what database?

hope this helps