DatasetError: Save during save

Is it possible to avoid the error ‘Unhandled promise rejection DatasetError: Operation (save) not allowed during save’? How can I check that the previous save/remove/new operation with a dataset is finished? Thanks.

@alexeychekanov have you found a solution?

I could also use an answer to this question. BUMP

If you directly use your databse collection instead of a dataset, you won’t have this issue.
in general, I think it always better not to work with datasets (less bugs, better performance etc…).

@jonatandor35 Thanks for the advice. I’ll see if it works.

According to the reference page, it seems like the onAfterSave() event handler should take care of this. Of course, I’m using it and getting the same error. So…if anyone can clarify, that would be great…

I ended up taking J.D.'s advice and skipped the dataset altogether for this and got everything working. I coded it all directly to my database using .insert. For me this is an initial registration form so I’m not sure if it will work with an update form where there is already information in the fields of the database

I’m going to try that now. It would be awesome to hear from a moderator on this as well though. Right now, it feels like I’ve been spinning my wheels with the dataset and now need to redo all of the work that was put into it. There’s probably something I’m missing, but it would be great to find out what and learn a little rather than thinking that I’ve wasted a lot of time on something that’s just buggy.

I know how you feel. I never worked with coding before this recent project and getting some help from a mod would have been great. Not sure that datasets are buggy; might just be that they’re pre-formatted chunks of code that don’t fit every situation. In my case, and likely yours, working with the raw database seems to be the best bet.

@dwyanekrzanowski Agreed, would just be lovely to have a definitive answer. Also, I just got the same error on a database for the first time.

@beyondobediencetrain I’m sure you’re more knowledgeable about coding than I am but if you want to share your code I can take a look. Can’t promise I’ll be much help but sometimes an extra pair of eyes can spot a minor mistake.

@dwyanekrzanowski Hey Dwayne, really appreciate the offer and agreed entirely. In this case though, I was just able to sort it out. I’m still very new to asynchronous code (and barely a novice with synchronous code) and that’s where my issue was in this case. I was performing an operation outside of a .then() that should have been inside. Maybe that’s consistent with this type of error, couldn’t say. In either case, that part at least is now resolved.

The same offer applies if you run into any other roadblocks, by the way.

Best,

Justin

@beyondobediencetrain Thanks! I’ll keep you in mind.

I’ve been running into this same problem with dataset save function, although for me it’s only happening when running on live site whereas my code works ok in preview.

Make sure that you have the correct collection permissions and that you have synced the sandbox collection to Live .

Hi Yisrael

I’d checked all of those things and couldn’t see any issues.

I’ve resolved the problem by using wixdata directly instead of dataset, but here is an example of the code

I’m working on an admin dashboard for my site (mouramoura.com) and have a repeater on the dashboard containing items that might be visible with a switch to enable/disable them.

The following code uses wixdata directly and works in both Preview and Live modes

export function switch1_click(event) {

$w('#dataset1').onReady( () => {            // check dataset is ready before processing 
    //console.log( "OnClick event"); 

    // get repeated item scope 
    let $item = $w.at( event.context ); 
    // get ID of the repeated item (switch1) which fired an event 
    const itemId = event.context.itemId; 

   //let clickedItemData = $item("#dataset1").getCurrentItem(); 
    let isChecked = $item("#switch1").checked;              // get new state of switch 

    //console.log( "onClick - Switch Status: " + isChecked ); 
    //console.log("onClick - Item state (before): " + clickedItemData.productVisible); 
    //console.log( "onClick - Item ID: " + itemId ); 

    
    // 
    // now save updated switch value to database directly  
    // 
    wixData.get( "Products",itemId) 
        .then( (item) => { 
            item.productVisible = isChecked; 
            wixData.update("Products", item ); 
        }) 
        .catch( (err) => { 
            let errorMsg = err; 
        }) 

}); 

}

Previously I was trying to get this working using the dataset.save() function with code as below (Note: written from memory - I deleted original code)

export function switch1_click(event) {

$w('#dataset1').onReady( () => {            // check dataset is ready before processing 
    console.log( "OnClick event"); 

    // get repeated item scope 
    let $item = $w.at( event.context ); 
   

    let clickedItemData = $item("#dataset1").getCurrentItem(); 
    let isChecked = $item("#switch1").checked;              // get new state of switch 

    console.log( "onClick - Switch Status: " + isChecked ); 
    console.log("onClick - Item state (before): " + clickedItemData.productVisible); 
	
	$item("#dataset1").setFieldValue("productVisible", isChecked );		// update value in dataset to switch status 

    $item("#dataset1").save; 

}); 

}

This code works fine in Preview mode, but when published to live it gives the
DatasetError: Operation (save) not allowed during save’ message

Note: I tried making it async, check for saving happening, etc but always got the same error. Eventually rewrote code to use wixData directly and problems solved.

It would be interesting to know what the actual problem was, as I spent quite some time trying to find what was going wrong.

Thanks
Paul

I was getting the same error.
I went to my live database, clicked “Sync Live to Sandbox” and then selected the “Erase Sandbox and replace it with all items from Live.” option. Then I previewed my page again and everything worked without errors.

Thanks for this answer. I’ve switched to using direct wixData updates in all my code now so hopefully wont see this issue any more, but useful to know about how you solved the problem in case I encounter it in the future

Getting error
UserError: datasetApi ‘save’ operation failed
Caused by DatasetError: Operation (save) not allowed during save

pls help me


import{ quoteRequest }from'backend/notifications';import wixCrm from'wix-crm';import{ session }from'wix-storage';import wixData from'wix-data';$w.onReady(function(){let product = session.getItem("product");//This line will get the item from storage session$w("#dataset1").onReady(()=>{  console.log("The product dataset is ready to be filtered with the following ID:"+ product);$w("#dataset1").setFilter(wixData.filter().eq("_id", product)//This line will filter the product dataset to find the 1 matching product from storage session).then(()=>{    console.log("The product is"+ product);$w('#formStrip').expand();//This line triggers the form strip to expand only if there are no errors in filtering dataset$w('#errorStrip').collapse();//This line triggers the error strip to collapse only if there are no errors in filtering dataset}).catch((err)=>{    console.log(err);$w('#formStrip').collapse();//This line triggers the form strip to collapse if an error occurs$w('#errorStrip').expand();//This line triggers the error strip to expand only if an error occurs});});//Our dataset for our form is called #requestDataset$w("#requestDataset").onBeforeSave(()=>{$w("#dataset1").onReady(()=>{let productFound =$w("#dataset1").getCurrentItem();//This line gets the current product from your Stores Dataset, in our example the Stores dataset is called #dataset1let theName = productFound.name;//This line gets the product namelet theImage = productFound.mainMedia;//This line gets the main image for the product //The following lines will set the values for these fields in the database$w("#requestDataset").setFieldValues({"productName": theName,//Make sure to replace the field key of each line to match your database"productId": product,//Make sure to replace the field key of each line to match your database"productImage": theImage //Make sure to replace the field key of each line to match your database});let checkFirst =$w('#firstName').valid;//Make sure to replace the fields with your form elements ID nameslet checkLast =$w('#lastName').valid;let checkPhone =$w('#phone').valid;let checkEmail =$w('#email').valid;//This next line checks to make sure all the validations we set in the user input settings are validif(checkEmail && checkPhone && checkLast && checkFirst){$w('#requestDataset').save();//This is the name of our form dataset$w('#errorText').hide();//This line hides the error message if all fields are valid}else{$w('#errorText').show();//This line shows the error message    console.log("Canceling save");returnfalse;//This line cancels the save function on the dataset}});});$w("#requestDataset").onAfterSave(()=>{quoteRequest();//This line triggers the notification from the backend.  In our example, the backend function is called quoteRequest .  console.log("A new notification was sent to all contributors.");$w('#formStrip').collapse();$w('#errorStrip').collapse();$w('#successStrip').expand();//This line triggers the form strip to expand after the form has been submitted.$w("#header1").scrollTo();//This line scrolls the page to the top header});});