Placing the Reference data value with code

Question:
I am retrieving a referenced value from a CMS table to place in another table. I can see in the console and through a variable I’ve placed on a form that the reference _id is accurately being capture. However, when the form is saved, the reference _id value doesn’t seem to be storing correctly. The tag in the CMS shows ‘Undefined’ as if it knows there should be an item referenced. Do you have any suggestions?

Product:
Wix Editor,

What are you trying to achieve:
I am creating a new item to place in a CMS table with a referenced field. Upon entering the Add Form, I am consuming in variables retrieved from other tables. One of those variables is a Referenced field. All variables on the form are validated as correct – both in the console and by display. But, when the form saves, it doesn’t save the Referenced field correctly.

What have you already tried:
I have read all information about referenced fields – get the referenced items _id, make sure it is in text form, etc, etc. None seems to resolve the issue.

Can you share the code that isn’t working?

Repasting the code to the correct entry. There are 2 pages that are a part of the user workflow here. It is the Add Activity page that the reference field is not storing correctly to the CMS.

Page 1: Second dynamic page connected to a list of Activity Items.

import wixMembers from ‘wix-members’;
import { currentMember } from ‘wix-members-frontend’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
import { session } from ‘wix-storage’;

$w.onReady(async () => {
const today = new Date();
const member = currentMember.getMember();
const itemID = session.getItem(“itemID”);
const itemFarm = session.getItem(“itemFarm”);
const itemYear = session.getItem(“itemYear”);
const itemFarmCode =session.getItem(“itemFarmCode”)
console.log(itemFarm, itemYear);

await $w('#dataset1').onReady( () => {
    console.log("dataset is ready")  

    $w("#farm").text = itemFarm

    // NOTE:  this code throws an error,  because the variables are still Null rather than having data.   Only a manual refresh on the URL works to retrieve the data.

   // NOTE:  Upon refresh, the Console shows the session variables have been retrieved and the  activity list for the selected farm is accurate. 


    $w("#dataset1").setFilter( wixData.filter().contains("title", itemFarm )
           .and(wixData.filter().contains("cropYear", itemYear)))

    $w("#editBtn").onClick( (event) => { 
        const $item = $w.at(event.context); 
        const itemId = event.context.itemId;
        const data = $w("#repeater1").data;
        const itemData = data.find((item) => item._id === itemId);
        const index = data.findIndex((item) => item._id === itemId);
    })

});
})

Page 2: This code is attached to a custom form for entering the activity that occurred on the Farm.
import { session } from ‘wix-storage’;
import wixWindow from ‘wix-window’;
import wixMembers from ‘wix-members’;
import { currentMember } from ‘wix-members-frontend’;

$w.onReady( async () => {
const today = new Date();
const itemID = session.getItem(“itemID”);
const itemFarm = session.getItem(“itemFarm”);
const itemYear = session.getItem(“itemYear”);
const itemFarmCode_id = session.getItem(“itemFarmCode_id”);
const itemFarmCode = session.getItem(“itemFarmCode”);
const itemMember = await currentMember.getMember().then( (member) => {
$w(‘#myMember’).value = member.loginEmail ;
return member })

   // NOTE:  The itemFarmCode_id and itemFarmCode are verified in the console as the correct referenced data.  Additionally, there are fields placed on the form that allow for visual reference.  But when the data is updated to the CMS, the tag for the referenced field says "Undefined".

    console.log(itemFarm, itemYear, itemMember, itemFarmCode_id, itemFarmCode);

$w("#dataset1").onReady(() => {
	$w('#farm').value = itemFarm;
	$w('#year').value = itemYear;
	$w("#farmCode").text = itemFarmCode;
	$w("#itemID").text = itemFarmCode_id
	$w("#dataset1").setFieldValue("title", itemFarm);
	$w("#dataset1").setFieldValue("cropYear", itemYear);
	$w("#dataset1").setFieldValue("member", itemMember.loginEmail);
	$w("#dataset1").setFieldValue("farmCode", itemFarmCode_id);

});

})