User edits on Lightbox form not being saved

I have a Lightbox that is called from a repeater that I want to use to allow a user to edit and save edits to an existing record in the data collection. I load the repeater with code rather than through the GUI and that works. When I click on an item in the repeater it passes the record ID and opens the Lightbox. That also works.

When I edit one of the text fields on the Lightbox and click on the save button the console shows that it is picking up the changes. But the changes are not saved to the collection. Can anyone help me understand how to make this work? A screenshot and my current code are shown below. Thanks!

export function btnSave_click(event) {
console.log( “Clicked Save” )
console.log( “===========” );
console.log($w( ‘#iptProjectDescription’ ).value);
console.log( “===========” );
let toUpdate = {
“projectName” : $w( ‘#iptProjectName’ ).value,
“projectDescription” : $w( ‘#iptProjectDescription’ ).value,
“projectDetail” : $w( ‘#iptProjectDetail’ ).value,
“projectCategory” : $w( ‘#iptProjectCategory’ ).value
};
wixData.update( “Projects2” , toUpdate)
.then( (results) => {
let item = results; //see item below
})
. catch ( (err) => {
let errorMsg = err;
});
wixWindow.lightbox.close();
}

Try to use SAVE instead of UPDATE…

import wixData from 'wix-data';


$w.onReady(()=>{
   $w('#btnSave').onClick(()=>{
      let toSave = {
         "projectName": $w('#iptProjectName').value,  
         "projectDescription": $w('#iptProjectDescription').value,  
         "projectDetail": $w('#iptProjectDetail').value,  
         "projectCategory": $w('#iptProjectCategory').value 
      };

      wixData.save("Projects2", toSave)
      .then( (results) => {
           let item = results; 
      })
      .catch( (err) => {
           let errorMsg = err;
      });
      wixWindow.lightbox.close();
   });
});

Hello,

Using SAVE will insert a new row of data if there is no ID in place, however, if the item’s ID is available then inside of the toUpdate add “_id” : //the items ID

“The update() function compares the _id property of the specified item with the _id property values of the items in the specified collection. If an item in the collection has that _id value, update replaced the item’s property values with the ones in the specified item. If the existing item had properties with values and those properties were not included in the specified item, the values in those properties are lost. The item’s _updatedDate property is also updated to the current date.”

import wixData from'wix-data';  

 $w.onReady(()=>{    
$w('#btnSave').onClick(()=>{      
 let toUpdate= {
	"_id" : //pass the items ID  
	"projectName": $w('#iptProjectName').value,            
	"projectDescription": $w('#iptProjectDescription').value,            
	"projectDetail": $w('#iptProjectDetail').value,            
	"projectCategory": $w('#iptProjectCategory').value        
	};        
	
	wixData.update("Projects2", toUpdate)      
		 .then((results) => {
			let item = results;})
		.catch((err) => {
			let errorMsg = err;
		});       
wixWindow.lightbox.close();    
	}); 
});

Thanks. Works great and makes perfect sense now that I see your solution.

Just a note for anyone else working on something like this be sure to include all fields in the collection as noted in the Wix documentation. Otherwise any entries in those fields will be lost.

Thanks again for your help