Answer the question
In order to leave comments, you need to log in
Why doesn't multer middleware run my route in express?
I am sure that the problem is in async await, but I don’t understand how to get rid of it, the middleware reaches UploadFileCtrl.uploads and after that there is a cyclic loading of images in the browser (pending), that is, after console.log the code is not executed,
I will be very grateful for the answer
app.post('/photos/upload', (req, res) => {
upload(req, res, (err: any) => {
if (err) return res.send({ error: 'invalid_file' })
console.log('hello from here')
UploadFileCtrl.uploads
})
})
class UploadFileController {
async uploads(req: express.Request, res: express.Response): Promise<void> {
const files: any = req.files
if (files) {
const urls = []
for (const file of files) {
const result = await uploadFromBuffer(file.buffer, 'Tweets')
urls.push(result)
}
res.status(200).json({
message: 'images uploaded successfully',
data: urls,
})
} else {
res.status(204).json({
err: 'Empty',
})
}
}
}
export const uploadFromBuffer = (buffer: Buffer, folder: string) => {
return new Promise((resolve, reject) => {
const stream = cloudinary.uploader.upload_stream(
{
folder,
},
(error: any, result: any) => {
console.log(error, result)
if (result) {
resolve({
url: result.url,
size: Math.round(result.bytes / 1024),
height: result.height,
width: result.width,
})
} else {
reject(error)
}
}
)
streamifier.createReadStream(buffer).pipe(stream)
})
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question