Insert reference field by code

Hi everyone,
I have a common issue with the insert of reference fields into a collection. I get always the error “reference is broken”.
Unfortunately there are many unsolved posts concerning this topic in the forum and I wonder if there is no one out there who knows the answers?
With one answer you are able to make many people happy :slight_smile:

https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-2
https://www.wix.com/corvid/forum/community-discussion/reference-field-broken
https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-3
https://www.wix.com/corvid/forum/community-discussion/reference-field-1
https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-1

I have two collections
1: GuestPhotos
photoid, photo, year, month_id, motif_id, uploadedby, description

2: Month
month_id, landuage_id, month_d, month_f

Now I’d like to insert a record with a new photo to the GuestPhotos collection.
Without referenced fields I make that this way:

let dataRecord = {
photoid : newPhotoId,
uploadedby : userName,
year : Number($w('#ddYear').value),
month_id : $w('#ddMonth').value,
motif_id : $w('#ddMotif').value,
description : $w('#tbDescription').value,
photo : imageUrl
};
//  Speichern des Datensatzes in der Collection
const insertResults = await wixData.insert("GuestPhotos", dataRecord)

}

I’ve already studied the wixdata.insertReference() method, but I can’t find the right way to use it.

I just found the solution! I’ve kept the normal insert statement, but I changed the values of the dropdowns.
I queried the field “_id” of the collection Month (not month_id!) and Motif, and filled the values as value for the corresponding dropdowns. Now the array with the data to insert looks like that:

  1. “18”
    photoid:

  2. “Urs Luginbuehl”
    uploadedby:

  3. 2019
    year:

  4. " 6b3a0e41-46ea-4ce7-8364-eba92bbccdfe"
    month_id:

  5. “3ae3e4ca-72da-4f75-b626-75b8876c2346”
    motif_id:

  6. “Description”
    description:

  7. “image://v1/246733_b27a21ac571a48499482fe588099220a~mv2.jpg/1400_787/246733_b27a21ac571a48499482fe588099220a~mv2.jpg”
    photo:

  8. “50d1b71a-1d7c-444f-a397-d744ef5bc8eb”
    _id:

  9. “246733f0-e5a4-4235-bd27-11e9ca5a946b”
    _owner:

  10. “2020-01-16T14:32:20.794Z”
    _createdDate:

  11. “2020-01-16T14:32:20.795Z”
    _updatedDate:

[4] and [5] are referenced fields _id’s .

Perhaps you can explain the topic more clear than I.
But anyway thanks for trying to help us.