I apologize for the flurry of questions but am having difficulties with a number of areas and this helps.
I am creating a meal plan from some data. Determining what I want is working just fine and I am writing this data to a JSON object in order to allow me to update a collection with the final data. However I have noticed strange behavior. There are a total of 21 items being added in every case Items 0-20. Items 1-6 always are blank, even though the data being written to it is not blank. Even though there is a random element it is always the same 6 positions that are blank even though the data in them is different each time it is run.
this is an example of the log of the data going into the system. The first object gets added correctly, the 2nd one does not.
This is what the final object looks like. It is always the same items 1-6 that are blank.
async function calculateMeals (mealPlan){
var ratioLow = [.15,.30,.8]
var ratioHigh = [.25,.4,.95]
var myID = 0
for (var day in days){
var remainingCals = goalCalories
mealPlan[days[day]] = {}
for (var i = 0; i < mealTypes.length; i++){
var lowCal = remainingCals * ratioLow[i]
var highCal = remainingCals * ratioHigh[i]
var mealType = mealTypes[i]
var mealChoices = []
console.log(mealTypes[i], ": minimum calories ", lowCal, " maximum calories ", highCal)
await wixData.query("Meals").hasSome("time", mealType)
.not(wixData.query("Meals").hasSome("allergens",$w('#allergies').value))
.find().then(async (mealList) => {
if (mealList.items.length > 0) {
console.log("Meal List",mealList)
for (var mealId = 0; mealId < mealList.items.length; mealId++){
var meal = mealList.items[mealId]
//console.log(meal.title,meal)
var multi = 1
while(meal.calories * multi <= highCal){
if (multi*meal.calories >= lowCal){
mealChoices.push({meal:meal._id,
servings:multi,
cals:meal.calories * multi,
day:day,
time:mealTypes2[i]
})
}
multi = multi + meal.multi
}
}
console.log("MealChoices are",mealChoices)
var randMeal = await getInt(mealChoices.length-1)
var finalMeal = mealChoices[randMeal]
console.log(days[day])
console.log(mealTypes[i])
console.log("chosen ",finalMeal,randMeal)
//mealPlan[days[day]][mealTypes[i]] = mealChoices[randMeal]
mealPlan[myID] = finalMeal
console.log("wrote", finalMeal, "to ID number" , myID)
myID = myID + 1
remainingCals = remainingCals - finalMeal.cals
console.log("remaining calories",remainingCals)
}
})
}
var weekRemainCals = weekRemainCals + remainingCals
}
return(mealPlan)
}
I am really slowing things down by adding a lot of awaits that I might not need so I likely need to fix that. Once I am finished creating this I am then updating the collection
mealPlan = await calculateMeals(mealPlan).then( async () => {
console.log("full mealplan",mealPlan)
console.log("Total remaining calories for week",weekRemainCals)
console.log("Mealplan length is",Object.keys(mealPlan).length)
for (var j = 0;j < Object.keys(mealPlan).length; j++){
await $w('#mealPlan').new().then(async ()=>{
console.log("adding new mealplan row",j,mealPlan[j])
await $w("#mealPlan").setFieldValues(mealPlan[j])
})
}
$w("#mealPlan").save()
is there a better way to do this? It is very very slow. It is seeming saving after each “setFieldValues” as the repeater that uses this data is refreshing after every single update