Submit to 2 collections with 1 submit button

Hi all. I have a user submission form that works fine but I want to be able to have the submission go to an archive collection as well when the user clicks the submit button. I’ve had it working in preview but it doesn’t seem to work on my live site. Here’s the code I used, just basically added the second wixData.save line, any ideas?

$w.onReady( function () {
$w(“#button2”).onClick((event) => {
let selectedTime = $w(“#dropdown2”).value.split(“:”);
selectedTime[0] = Number(selectedTime[0]) * 3600000;//converts hours to milliseconds
selectedTime[1] = Number(selectedTime[1]) * 60000;//converts minutes to ms
let finalTime = new Date( new Date().getTime() + selectedTime[0] + selectedTime[1]);

let toSave = {
“dateTimer”: finalTime,
“description”: $w(“#input1”).value,
“photo1”: $w(“#image1”).src,
“photo2”: $w(“#image2”).src,
“photo3”: $w(‘#image3’).src,
“photo4”: $w(‘#image4’).src,
“photo5”: $w(‘#image5’).src,
“collection”: $w(‘#dropdown1’).value,
“downloadable”: $w(‘#checkbox2’).checked,
“ip”: $w(‘#text38’).text
}//etc. etc.
wixData.save(“Timedout”, toSave),
wixData.save(“archive”, toSave)

.then(() => { 
   wixLocation.to("/all");   //<<<<< PUT IT HERE    

})
})
})
})

Any help with this?

Not sure if this’ll help but maybe try having two separate “let toSave” functions and basically copy/paste the values to save. If that doesn’t work then try having two “onClick” events (one for the regular one, other for archive).

Let me know if that works, curious to see!

Thanks,
Luke

The separate let toSave functions didn’t work but adding 2 onClick events did. Thanks for the advice!

@ibateman37 ,
Use Promise.all :

 Promise.all([wixData.save("Timedout", toSave), wixData.save("archive", toSave)])
.then(() => {
//the code you want to execute
})

@jonatandor35 I’m getting a ‘parsing error unexpected token )’ when I use the code. Where exactly should I put it in the code above.

@ibateman37 instead of this code:

wixData.save("Timedout", toSave),
wixData.save("archive", toSave)
.then(() => {
    wixLocation.to("/all");   //<<<<< PUT IT HERE
})

Use the Promise.All(). And of course, add the wixLocation.to() to the .then() part.
The Promise.all will make sure it won’t leave the page until both save() actions are successfully done.

@jonatandor35 Replaced the above with your code but I’m getting Parsing error : unexpected token ]

@ibateman37 So you did something wrong somewhere in your code.

Close the parentheses of the second save:

Promise.all([wixData.save("Timedout", toSave), wixData.save("archive", toSave)])
.then(() => {
//the code you want to execute
})

@jonatandor35 Thanks, that’s working great now