hi i have this issue where i keep getting 500 response error from postman and angualr when i try to tigger this method
exports.uploadImage = async (req, res) => {
try {
console.log(req.body)
if (!req.file) {
throw new Error('No file received in the request.');
}
const file = req.file;
const fileName = `${Date.now()}-${file.originalname}`;
const fileUpload = storage.bucket().file(`images/${fileName}`);
const blobStream = fileUpload.createWriteStream({
metadata: {
contentType: file.mimetype,
},
});
blobStream.on('error', (error) => {
throw new Error(error);
});
blobStream.on('finish', async () => {
const downloadURL = await fileUpload.getSignedUrl({
action: 'read',
expires: Date.now() + 1000 * 60 * 60 * 24 * 365 * 2, // 2 years from now
});
res.status(200).send({ imageUrl: downloadURL[0] });
});
blobStream.end(file.buffer);
} catch (error) {
console.error("Error uploading image:", error);
res.status(500).send({ error: error.message });
}
};
and i calling this method like this
app.post('/images', upload.single('file'), productController.uploadImage);
i try to console.log(req.body) and the resposne was [Object: null prototype] {}
Error uploading image: Error: No file received in the request.
so i try to send the req with angular app and still the same error 500 here is an image from both postman and angualr enter image description here enter image description here
and here how i send the method using angular
async uploadImage() {
if (!this.selectedFile) {
console.error('No file selected.');
return;
}
const formData = new FormData();
formData.append('file', this.selectedFile);
console.log(this.selectedFile)
console.log(formData)
try {
const response = await axios.post('http://127.0.0.1:5001/rebarsalon/us-central1/api/images', formData);
console.log('Image uploaded successfully:', response.data);
// Handle the response as needed, e.g., display a success message or update the product with the image URL
} catch (error) {
console.error('Error uploading image:', error);
// Handle the error, e.g., display an error message to the user
}
}