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);
});
})