I want to obtain the submission ID from a wix form.
The code runs well in the preview dev tools, but on the live site the request is pending for about 14 seconds and then it returns an ExecutionTimeoutError error:
{
"result": {
"message": "Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.",
"name": "ExecutionTimeoutError",
"stack": "Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.",
"code": "EXECUTION_TIMEOUT",
"_elementoryError": true
},
"exception": true
}
Backend module:
import wixData from 'wix-data';
async function checkSubmission(email) {
const minDate = new Date(new Date().getTime() - 2 * 60000);
return wixData.query('dataset_name')
.eq('email', email)
.gt("_createdDate", minDate)
.descending('_createdDate')
.limit(1)
.find()
.then((results) => {
if (results.items.length > 0) {
return results.items[0];
} else {
return null;
}
});
}
export async function getSubmissionId(email) {
let attempts = 0;
const executePoll = async (resolve, reject) => {
const result = await checkSubmission(email);
attempts++;
//console.log('- poll', attempts, result);
if (result !== null) {
return resolve(result._id);
} else if (attempts == 10) {
return reject(new Error('Exceeded max attempts to get submission'));
} else {
setTimeout(executePoll, 200, resolve, reject);
}
}
return await new Promise(executePoll)
.then(id => {return {"id": id}})
.catch(error => {return {"id": null}});
}
Frontend:
import { getSubmissionId } from "backend/main.jsw"
export function wixForms1_wixFormSubmitted(event) {
let email = event.fields[2].fieldValue;
//console.log('getSubmissionId', email);
getSubmissionId(email).then(result => console.log(result));
}