Duplicate and blank entries on data

I’ve made a comment form that has the options to the user fill their name, their picture, rate the product and leave a comment, and also the data automatically refresh after hitting the submit button, so the comment appears below on a comment section. The fields of the comment form are connected to the dataset1 and the fields of the comment section are connected to the dataset 2. Both of them have the same database, QGisB.
When I was doing this code without the name and the picture options, the refresh was working fine, but after I’ve incluid them in the code (what I probably did wrong, but I’m not seeing where now), the data started to duplicate, but with the picture and the name blank, and the refresh only works to see the first buggy comment, in order to see the second correct one, you need to refresh the entire page.

Plus, it’s appearing to be working only on the Editor now. I can’t submit any data on the published page.
Here’s the code:

import wixData from 'wix-data'
 
export async function submit_click(event) {
await saveComment(), $w('#dataset2').refresh()
 
async function saveComment (parameter) {
const newComment = $w('#commentbox').value
const newDate = new Date().toLocaleDateString()
const profilePic = $w('#uploadButton1').value
const name = $w('#nickbox').value
const rating = $w('#ratingbox').value

let toSave = {
"nick": name,
"date": newDate,
"pic": profilePic,
"comment": newComment,
"rating": rating,
};
 
await wixData.save("QGisB", toSave)
.then( (results) => {
if(results.items.length > 0) {
let firstItem = results.items[0];
$w('#dataset2').refresh()
}
else { }
})
.catch( (err) => {let errorMsg = err;});
}
}

I’ll be really glad if anyone could help me! :wink:

Mixing wixData functions with dataset functions is more trouble than it’s worth usually. You should settle on one approach or the other.

The wixData.save code, since you are not passing the _id value, will always create a new record. The submit button is creating a record too if tied to the dataset.

Using dataset functions, you could do something like this in place of the wixData.save code:

 $w("#dataset2").setFieldValues(toSave);
 $w("#dataset2").save();

You will have to refine this no doubt, but you should be beyond the duplicate/blank problem.

I hope this helps.