insertreference() and multireference fields

Hi there,

I have been trying to insert the user filling in a form as a reference when he submits it. looking at the wix api I came up with the following code

export function button2_click(event) {

    currentMember.getMember(options)
      .then((member) => 
        {

            const id = member._id;
            const loggedIn = member ? true : false;
            const loginEmail = member.loginEmail;
            const contactEmails = member.contactDetails.emails;
          
            let UserEmail = [];
            UserEmail.push(loginEmail);

            let UserID = [];
            UserID.push(id);

            console.log(UserID)

            
            let itemObj = $w("#dataset2").getCurrentItem();
            console.log(itemObj._id)

            wixData.insertReference("Members/PrivateMembersData", "test", itemObj._id , "4082af76-e744-4fe6-a025-2ca88ffc19c6")
              .then( () => {
                console.log("Reference inserted");
              } )
              .catch( (error) => {
                console.log(error);
              } );

 
            
            $w("#dataset2").save();

           
            
            $w('#text84').text = loginEmail;
            $w('#text85').text = id;
            //$w('#text91').text = val;

        })

this code does not work and I cat get my head around how it would know in which row the item should be referenced, I have also tried filling in the fieldkey instead of itemobj._id but to no evail.
Does anyone know how to solve this issue, thank you.

You are not able to insert anything into the private members database. This is a read only database. Read more about it here

Hi @jarod , I was aware of this. I am not trying to insert anything in the private members database but trying to do the opposite and linking the name to the test database. according to the api this is the way to do that or am I wrong?

Does the console throw an error message? If so what is it. Could you send the link for the form so I can test?

@jarod
nope and it even says the reference is inserted


https ://www. byrssa. io/account /test
this is the link but its behind a members portal

Hi brov, did you find a solution do this?

@jarod
@empro2pro

Found the solution thaks to you Jarod and with a little help of wix devs.
to make things short, this was the actual code I need.

            let itemObj = $w("#dataset2").getCurrentItem();
            console.log(itemObj._id)
            wixData.insertReference("test", "people",  itemObj._id , member._id)

It basically came down to me not understanding the insertReference documentation where I thought you had to first reference the original collection, then the current collection and then both ID’s of the items. But apperantly velo has enough with the ID of the item that you want to refer. This is the original code the wx dev team gave me but this would have just created another item which I didnt want.

The advanced department has reviewed the case and got back to us with the clarifications that you should only use insertReference() when adding items to the reference field. Insert() works with the regular reference fields instead.
Your code would look similar to this (with your own IDs and values, of course):
let toInsert = {
       "title": $w('#input1').value,
   }

   wixData.insert("test", toInsert)
   .then( (results) =>{
       console.log("INSERT", results)
       wixData.insertReference("test", "people", results._id, '816f7f9a-d347-47f4-9c46-d98507fd984b')
   })

For the Wix team I would suggest to change the explenation of this piece of text

Thank you

3 Likes