What am I doing wrong with InsertReference?

Hi Folks!

Hope you’re well. I’ve been facing issues with correctly inserting a reference between my databases. Code is below but some context:

● The aim here is insert a reference into the “member” field of the Portfolios database.
● The item being referenced should be the ID of the current item within the Members Dataset.
● The item for the reference to be placed within should be current item within the Portfolios Dataset, in the “member” field.

Could anyone share some light here? I’ve checked and all fields correctly correspond to their correct attributes etc. Happy to share my site if necessary.


$w.onReady(function () {

$w("#portfoliosData").onReady( () => {
 let MembCurrentItem = $w("#membersData").getCurrentItem(); 

 let PortCurrentItem = $w("#portfoliosData").getCurrentItem();
 
wixData.insertReference("Portfolios", "member", PortCurrentItem._id, MembCurrentItem._id)

  .then( () => {
    console.log("Reference inserted");
  } )

  .catch( (error) => {
    console.log(error);
  } )

  }
            )
} )

As always, huge thanks in advance!
Phil

Hi Philip,

You might try putting a console.log after the two getCurrentItem function calls to be sure that you are grabbing the records that you think you are.

Secondly, mixing dataset functions with wixData functions on the same page can be tricky. What may be happening is that the reference gets inserted into the collection just fine, but it may get overridden by the dataset save command. When you write to the collection with a wixData command, the dataset knows nothing about that and remains unchanged and later the original state of the reference field (empty presumably) is reverted with the dataset save.

Thanks Anthony, you make a great point about mixing datasets with Corvid - I’ll aim to find a workaround. Cheers!