Wix表单获取提交ID时触发ExecutionTimeoutError:预览正常但线上环境请求超时
Hey there, let's break down why your code works smoothly in preview but throws an ExecutionTimeoutError in the live Wix site, and walk through how to fix it.
The Problem Recap
Your code to fetch a form submission ID runs fine in the preview dev tools, but in the live site it hangs for ~14 seconds before returning this 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 }
Here's your current code for reference:
Backend (backend/main.jsw)
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)); }
Why This Happens
Backend Polling Breaks Wix's Execution Limits
Wix backend modules (.jsw files) have strict execution time limits (usually ~15 seconds in live sites). Your current code runs a polling loop inside the backend function—each loop waits 200ms, and with up to 10 attempts, this adds up to nearly 2 seconds of wait time, plus the database query time per attempt. It's just barely hitting the timeout threshold in live mode (preview has looser limits).Potential Database Query Slowness
If yourdataset_nameis large and doesn't have an index onemail+_createdDate, each query takes longer, pushing the total execution time over the limit.
Fixes to Try (Ordered by Priority)
1. Grab the Submission ID Directly from the Form Event (Best Option)
You might not even need to query the database! Wix's form submission event often includes the submission ID directly. First, log the full event to see what's available:
export function wixForms1_wixFormSubmitted(event) { // Log the entire event to inspect its structure console.log('Full submission event:', event); // Check common fields for the submission ID (adjust based on your log output) const submissionId = event.submissionId || event._id || event.data?._id; if (submissionId) { console.log({"id": submissionId}); } else { // Fall back to polling if needed (use the optimized version below) } }
This is way more efficient than querying the database—if the ID exists in the event, you're done immediately.
2. Move Polling to the Frontend
If you still need to poll the database, move the loop to the frontend. Each poll will be a separate, short-lived backend request, avoiding the single backend function timeout.
Updated Backend (backend/main.jsw)
import wixData from 'wix-data'; // Export the query function directly export async function checkSubmission(email) { const minDate = new Date(new Date().getTime() - 2 * 60000); const results = await wixData.query('dataset_name') .eq('email', email) .gt("_createdDate", minDate) .descending('_createdDate') .limit(1) .find(); return results.items.length > 0 ? results.items[0] : null; }
Updated Frontend
import { checkSubmission } from "backend/main.jsw" export async function wixForms1_wixFormSubmitted(event) { const email = event.fields[2].fieldValue; let attempts = 0; const maxAttempts = 10; const pollInterval = 200; const pollForSubmission = async () => { attempts++; const submission = await checkSubmission(email); if (submission) { console.log({"id": submission._id}); return; } if (attempts >= maxAttempts) { console.log({"id": null}); return; } // Wait and try again setTimeout(pollForSubmission, pollInterval); }; // Start polling pollForSubmission(); }
3. Optimize Database Query Speed
Add a composite index to your dataset to speed up the query:
- Go to your Wix Dashboard → Databases → Open
dataset_name - Click the Indexes tab
- Create a new composite index with:
- First field:
email(match type: Equal) - Second field:
_createdDate(match type: Range)
This will make youreq()+gt()query run much faster, reducing the time per poll.
- First field:
Final Notes
Always test changes in a staging version of your Wix site first before pushing to live. If you still run into issues, check Wix's site monitoring logs (under Site Actions → Monitoring) for more specific timeout details.
内容的提问来源于stack exchange,提问作者Ciprian Coman

