@archaeous
Tried to reconfigure your CODE, to make it perhaps a little bit better readable.
//import Chart from 'chart.js'
import wixData from 'wix-data'
import wixUsers from 'wix-users'
import wixWindow from 'wix-window'
import {memory} from 'wix-storage'
import wixLocation from 'wix-location';
var DATASET1 = "myPlan"
var DATASET2 = "snacks"
var myInfo, toInsert = {}, attempts = 0, success = false, abortRun = 0;
$w.onReady(async()=>{
updateHTML();
myInfo = await wixData.query("memberPlannerInfo")
.eq("_owner",wixUsers.currentUser.id).find()
if (myInfo.items.length > 0){
toInsert = myInfo.items[0]
$w('#allergies').value = myInfo.items[0].myAllergies
$w('#dietType').value = myInfo.items[0].dietType
$w('#height').value = myInfo.items[0].height
$w('#weight').value = myInfo.items[0].weight
$w('#age').value = myInfo.items[0].age
} else {toInsert._owner = wixUsers.currentUser.id}
$w('#'+DATASET1).onReady(async()=>{
var dt = new Date();
await $w('#'+DATASET1).setFilter(wixData.filter()
.gt("time", 0)
.lt('time', 4)
.eq("_owner", wixUsers.currentUser.id)
.eq('day', dt.getDay())
)
if ($w('#'+DATASET1).getTotalCount()===0) {$w('#columnStrip4').expand();}
});
//*********** onClick-Control-Section ****************************************
//--------- New Meal-Plan ----------
$w('#NewMealPlan').onClick(()=>{$w('#box1').expand();});
//--------- Get Meal-Plan ----------
$w('#GetMealPlan').onClick(async()=>{console.log("starting plan");
wixWindow.openLightbox("Welcome");
attempts = 0
while (attempts <3 && success == false){await startPlan();}
//--------------------
toInsert.myAllergies = $w('#allergies').value
toInsert.dietType = $w('#dietType').value
toInsert.calories = parseInt($w('#totalCalories').text,10)
toInsert.height = parseInt($w('#height').value,10)
toInsert.weight = parseInt($w('#weight').value,10)
toInsert.age = parseInt($w('#age').value,10)
//--------------------
if (myInfo.items.length > 0){
toInsert.email = await wixUsers.currentUser.getEmail()
await wixData.update("memberPlannerInfo",toInsert)
}
else{console.log("added a new one by toInsert")
toInsert.howCreated = "toInsert"
toInsert.email = await wixUsers.currentUser.getEmail()
await wixData.insert("memberPlannerInfo",toInsert)
}
});
//--------- ????? ----------
$w('#image1').onClick(async(event,$w)=>{console.log("starting plan");
//let $item = $w.at(event.context)
const myItem = await $w('#'+DATASET1).getCurrentItem()
//console.log(myItem)
memory.setItem("MealDash", myItem._id)
wixLocation.to(myItem.meal['link-meals-title'])
});
//--------- Custom-Plan ----------
$w('#customPlan').onClick(async(event)=>{//manually trigger to restart.
//Populates initial input fields with data from their previous entry,
but replaces calories with their custom entry.
myInfo = await wixData.query("memberPlannerInfo")
.eq("_owner",await wixUsers.currentUser.id)
.find()
toInsert = myInfo.items[0];
//console.log("seting selction tags to ",myInfo.items[0].myAllergies)
$w('#allergies').value = myInfo.items[0].myAllergies
$w('#dietType').value = myInfo.items[0].dietType
$w('#height').value = myInfo.items[0].height
$w('#weight').value = myInfo.items[0].weight
$w('#age').value = myInfo.items[0].age
toInsert.calories = parseInt($w('#customCalories').value,10)
$w('#totalCalories').text = $w('#customCalories').value
console.log("using this data",toInsert)
toInsert.howCreated = "CustomPlan"
wixData.update("memberPlannerInfo",toInsert)
$w('#box1').collapse();
await deleteMeals();
attempts = 0, success = false
while (attempts <3 && success == false){await startPlan()}
});
//--------- Custom-Plan-Over ----------
$w('#StartOver').onClick(async(event)=>{
//Similar to above but starts completely over & lets people pick initial values again
await deleteMeals()
$w('#columnStrip4').expand()
$w('#box1').collapse()
wixWindow.scrollTo(0, 0)
});
});
//---------- FUNCTIONS --------------------------------------------------------
async function updateHTML(){/*DO SOME STUFF here */}
async function calculateMealsNew (mealPlan){/*No Data stuff here*/}
async function startPlan(){
await calculateMealsNew(mealPlan);
if (abortRun == 0){success=true;} else{abortRun = 0;}
/*THE INSERT PART */
if (success == true){
await wixData.bulkInsert("myMealsTest", mealPlan);
$w('#'+DATASET1, '#'+DATASET2).refresh();
$w('#columnStrip4').collapse();
wixWindow.scrollTo(0, 0); updateHTML();
}
}
async function deleteMeals(){
let removeList = []
let myMeals = await wixData.query("myMealsTest")
.eq("_owner",wixUsers.currentUser.id)
.find()
for (let i = 0; i<myMeals.items.length;i++){
removeList.push(myMeals.items[i]._id)
}
wixData.bulkRemove("myMealsTest",removeList)
}
Perhaps with a little bit better readable structure you will be able to find your issue. USE MORE CONSOLE_LOGS!!! → Use them almost on every of your code-lines, to inspect every single line of your code.