I am manually creating entries in collections via code. Since I can’t use standard elements as the input is used for both first time input, as well as updating values. However I am getting consistently extra blank entries and I am not sure why.
I have removed a large part of my code so it is easier to follow as the vast majority of it has nothing to do with inserting data. The part in question is the “memberplannerinfo” collection. I get duplicate entries for the same ownerid and for the life of me can’t figure it out. It has never happened to me on sandbox. And I don’t think it always happens on live site as I would expect far more duplicates if it did.
Main idea is that users create initial content. If they do not have an entry, create an initial object with their ownerid as a key field. If there is data set this object = the already existing data. This object is called toInsert.
Then I update toInsert with various data depending on what they are doing. OR I update data with information from this object. Every place I either update, or insert I have added some things for “how” the record should be created/updated. However the blank rows are just completely blank except for system generated fields, ownerid, _id, created date etc…
Can anyone see why I am getting extra rows of junk data?
//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 toInsert = {}
var myInfo
var attempts = 0
var success = false
var abortRun = 0
$w.onReady(async function () {
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
}
});
async function updateHTML(){
//DO SOME STUFF
}
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('#myPlan').refresh()
$w('#snacks').refresh()
$w('#columnStrip4').collapse()
wixWindow.scrollTo(0, 0)
updateHTML()
}
}
export async function myPlan_ready() {
var dt = new Date()
await $w('#myPlan').setFilter(wixData.filter()
.gt("time",0)
.lt('time',4)
.eq("_owner",wixUsers.currentUser.id)
.eq('day',dt.getDay())
)
if ($w('#myPlan').getTotalCount() === 0) {
$w('#columnStrip4').expand()
}
}
export async function GetMealPlan_click(event) {
//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)
}
}
/**
* Adds an event handler that runs when the element is clicked.
* @param {$w.MouseEvent} event
*/
export async function image1_click(event,$w) {
//let $item = $w.at(event.context)
const myItem = await $w("#myPlan").getCurrentItem()
//console.log(myItem)
memory.setItem("MealDash", myItem._id)
wixLocation.to(myItem.meal['link-meals-title'])
}
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)
}
export async function customPlan_click(event) {
//Someone can 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()
}
}
export async function NewMealPlan_click(event) {
$w('#box1').expand()
}
export async function StartOver_click(event) {
//Similar to above but just starts completely over and lets people pick initial values again
await deleteMeals()
$w('#columnStrip4').expand()
$w('#box1').collapse()
wixWindow.scrollTo(0, 0)
}