Dynamic Dataset 'add' operation failed. It not allowed during save

I want to add some new items to dataset (#dynamicDataset).
I can add first new item by using add() function and setFieldValues function but it’s trouble from the second. There is Error like below:
My code:


Why I can’t use add function more after adding first item?
When I change this code to :


There is same error.
How can I solve this?

Thanks for your help.

Because the add() method that creates a promise that saves the current item and adds another one, this other has to be saved before the next one can be added, and so on…

You should chain the add() method with the setFieldValues() after the promise is completed, like so:

function addItem(result) {
  let d = $w('#dynamicDataset')
  d.add()
    .then(() => console.log('added'))
    .setFieldValues(result)
    .save()
    .then(() => console.log('saved'))
}