Save record in Dataset

I have a code based form to enter or update records in my Collection.

The database is searched using a unique key, the record fields are then displayed in aTable
and in Text Input records, where they can be edited

I also use + Upload Image button to add/change an Image

Finally I use SAVE (as in Wix Dataset api) to update the Dataset.

Most of the time this works just fine - but occasionally the Save creates a new record so I get two (different) records with the same key.

This failure mostly occurs when I change the Image - only occasionally when changing a Text field.

All comments welcome

Thank-you

Hey
Please insert your code here to get help with that. Insert / Paste your code, then select all your code and choose SCRIPT which is the icon on the toolbar at the far right. Then we can try to help you, without any code helping you won’t be easy.

OK - Here is my code (I did not understand your ref to SCRIPT on the far right) so it is a copy and paste.

The Editable Text boxes and upload image are connected to the Dataset - the searchResults Table is free standing.

Thank-you for your interest.

David

// For full API documentation, including code examples, visit [u]http://wix.to/94BuAAs[/u]
import wixData from ‘wix-data’;
$w.onReady( function () {
//TODO: write your page related code here…
});
export function searchButton_click(event) {
//Add your code for this event here:
wixData.query(“Stock”)
// Query the collection for any items whose “Name” field contains
// the value the user entered in the input element
.eq(“title”, $w(“#StockNumber”).value)
.find() // Run the query
.then(res => {
// Set the table data to be the results of the query

$w("#searchResults").rows = res.items;  
$w("#Index").value =  res.items[0]._id , 
$w("#StockNumber").value = res.items[0].title; 
$w("#Description").value = res.items[0].description; 
$w("#Price").value = res.items[0].price; 
$w("#NewIn").value = res.items[0].newIn; 
$w("#Stock").value = res.items[0].stock; 
$w("#Sold").value = res.items[0].sold; 

})
. catch ((error) => {($w(“#StockNumber”).value = “#InvalidStockNumber”)
});
}

export function saveButton_click_1(event) {
$w(“#StockData”).setFieldValues( {
“title”: $w(“#StockNumber”).value,
“price”: $w(“#Price”).value,
“description”: $w(“#Description”).value,
“stock”: $w(“#Stock”).value,
“newIn”: $w(“#NewIn”).value,
“sold”: $w(“Sold”).value} );
$w(“#StockData”).save();
}

export function removeButton_click(event) {
//Add your code for this event here:
$w(“#StockData”).remove()
. catch ( (err) => {
let errorMsg = err;
console.log(errorMsg);
});

}

Hey
I can’t see any obvious errors but you wrote “This failure mostly occurs when I change the Image” and I can’t see any code that has to do with any image uploading connected to this dataset.

@andreas-kviby

I have included a ‘+ Upload Image’ Button connected to the Image field in the dataset.

Click on this button brings up access to my folders where I Choose an image.

This image is uploaded into the selected record in the Dataset as part of the save() operation.

I have a feeling that this could well be some weird form of ‘bug’ since it is all OK most of the time!