CaptainHoola
New Coder
Hello,
I've created a audio recorder for my website which lets the user send a recording to a google drive account. At the moment i'm running this on a local environment before i deploy online. For all intents and purpose, everything seems to be running as expected. The user records audio, clicks upload then i receive the file in the google drive account. However, i'm getting the following error once the Upload button is pressed:
Any ideas?
Thanks,
line116 = line24,
line133 = line41,
FRONTEND
BACKEND
I've created a audio recorder for my website which lets the user send a recording to a google drive account. At the moment i'm running this on a local environment before i deploy online. For all intents and purpose, everything seems to be running as expected. The user records audio, clicks upload then i receive the file in the google drive account. However, i'm getting the following error once the Upload button is pressed:
main.js:133 Error during upload: Stack: TypeError: Failed to fetch
at HTMLButtonElement.<anonymous> (http://127.0.0.1:5500/main.js:116:32)
Any ideas?
Thanks,
line116 = line24,
line133 = line41,
FRONTEND
JavaScript:
uploadBtn.addEventListener('click', async function() {
// Ensure there is an audio file to upload
if (!playback.src) {
alert('No recording found. Please record audio before uploading.');
return;
}
// Log playback source for debugging
console.log('playback.src:', playback.src);
try {
// Fetch the blob from the playback source
const blob = await fetch(playback.src).then(res => res.blob());
console.log('Blob fetched successfully:', blob);
const formData = new FormData();
const timestamp = new Date().toISOString().replace(/[:.-]/g, ''); // Unique filename
const filename = `recording_${timestamp}.ogg`; // Use .ogg format to match the type
formData.append('file', blob, filename);
console.log('FormData prepared:', [...formData.entries()]);
// Send the blob to the backend server
const response = await fetch('http://localhost:3000/upload', {
method: 'POST',
body: formData,
});
if (!response.ok) {
const errorText = await response.text(); // Get the response text to debug
throw new Error('Server responded with status ' + response.status+ ': ' + errorText);
}
const data = await response.json();
console.log('Upload successful:', data);
alert('Upload successful! File ID: ' + data.fileId);
} catch (error) {
// Log error message and stack trace for debugging
console.error('Error during upload:', error, 'Stack:', error.stack);
alert('An error occurred during the upload: ' + error.message);
}
});
BACKEND
JavaScript:
// Respond with the file ID
const responseData = { fileId: response.data.id };
console.log('Response to be sent:', responseData); // Log the response data
res.status(200).json({ fileId: response.data.id }); // Respond with the file ID
} catch (error) {
console.error('Error during file upload:', error);
res.status(500).json({ message: 'Failed to upload file.', error: error.message });
}
});
