OK…this is really bizarre.
I have code to build a CSV from an array of data that runs fine in preview in the editor but crashes in a browser: Request failed with status code 413
I would have expected this to be a file size related error but the file size is tiny - about 250KB when it completes in preview mode.
So here’s the code. I am confident that there’s nothing wrong with it as it runs in preview perfectly and, as long as I limit the number of records I send to it, it runs fine in a live browser too. I can send 615 records through it in a browser but at 620 records, it gives me that error. It’s not a data problem as when I slice the array to target where it fails, it still runs well, beyond the 615th record, when I slice the array from the 600th.
This is the code:
export const generateMembershipCsvFromContacts = webMethod(Permissions.Anyone, async (contactsArray) => {
const headers = [
"Title",
"First Name",
"Last Name",
"Member Number",
"Email",
"Phone",
"Number of Journals",
"Member Type",
"Family Member Number",
"Addr1",
"Addr2",
"Addr3",
"Addr4",
"Addr5",
"Addr6",
"Addr7",
"Date Address Changed",
"Date Email Changed",
"Life Time Sub?",
"Amount Last Paid",
"Payment Method",
"Payment Status",
"Journal Type",
"Last Payment Date",
"Gift Aid?",
"Renewal Date",
"Payment Comments",
"Joined Date",
"Deceased Date",
"Notes"
];
console.log("Starting CSV generation");
let csvContent = headers.join(",") + "\n";
contactsArray.forEach(contact => {
//console.log("Processing contact with ID:", contact._id);
let email = contact.info.emails && contact.info.emails.length > 0 ? contact.info.emails[0].email : "";
let phone = contact.info.phones && contact.info.phones.length > 0 ? contact.info.phones[0].phone : "";
let row = [
contact.info.extendedFields["custom.title"] || "",
contact.info.name.first || "",
contact.info.name.last || "",
contact.info.extendedFields["custom.membernumber"] || "",
email,
phone,
contact.info.extendedFields["custom.numberofjournals"] || "",
contact.info.extendedFields["custom.type"] || "",
contact.info.extendedFields["custom.otherfamilynumber"] || "",
contact.info.extendedFields["custom.addr-1-house-name"] || "",
contact.info.extendedFields["custom.addr2"] || "",
contact.info.extendedFields["custom.addr3"] || "",
contact.info.extendedFields["custom.addr4city"] || "",
contact.info.extendedFields["custom.addr5statecounty"] || "",
contact.info.extendedFields["custom.addr6postcodezip"] || "",
contact.info.extendedFields["custom.addr7country"] || "",
contact.info.extendedFields["custom.dateaddresschanged"] || "",
contact.info.extendedFields["custom.dateemailchanged"] || "",
contact.info.extendedFields["custom.lifetime-subscription"] || "",
contact.info.extendedFields["custom.amountlastpaid"] || "",
contact.info.extendedFields["custom.paymentmethod"] || "",
contact.info.extendedFields["custom.paymentstatus"] || "",
contact.info.extendedFields["custom.journaltype"] || "",
contact.info.extendedFields["custom.lastpaymentdate"] || "",
contact.info.extendedFields["custom.giftaid"] || "",
contact.info.extendedFields["custom.renewaldate"] || "",
contact.info.extendedFields["custom.paymentcomments"] || "",
contact.info.extendedFields["custom.joineddate"] || "",
contact.info.extendedFields["custom.deceaseddate"] || "",
contact.info.extendedFields["custom.notes"] || ""
].join(",");
csvContent += row + "\n";
});
console.log("CSV content prepared, starting upload...");
const csvBuffer = Buffer.from(csvContent);
try {
let uploadResult = await uploadCSV(csvBuffer);
console.log("Upload successful, file URL:", uploadResult.fileUrl);
return uploadResult.fileUrl;
} catch (error) {
console.error("Error during CSV upload:", error.message);
throw error; // re-throw the error to ensure it gets caught by any caller
}
});
async function uploadCSV(buffer) {
console.log("Preparing to upload CSV file. Buffer size:", buffer.length, "bytes");
try {
let result = await mediaManager.upload(
"/CSVdownloads",
buffer,
"Contacts.csv", {
"mediaOptions": {
"mimeType": "text/csv",
"mediaType": "document"
},
"metadataOptions": {
"isPrivate": false,
"isVisitorUpload": false
}
}
);
console.log("CSV upload successful. File URL:", result.fileUrl);
return result;
} catch (error) {
console.error("CSV upload failed:", error.message);
throw error; // Re-throw the error to ensure it gets caught by the caller.
}
}
Does anyone have any idea where I should look to troubleshoot? I am stumped.
I am logged in on the same account in preview and a chrome browser…so I can’t see that it would be permissions related…it’s just plain WEIRD.
Thanks,
Simon.