i am puzzled with this problem. this taken my a lot time . yet not figure out any solution.
can you anyone can help me for this problem ?
I tried many things from stack overflow but didn't find any solution.
this is my file.js
require('dotenv').config()
const express = require('express').Router();
const multer = require('multer');
const path = require('path');
const File = require('../models/file');
const { v4: uuid4 } = require('uuid');
let storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, 'uplods/'),
filename: (req, file, cb) => {
const uniqueName = `${Date.now()}-${Math.round(Math.random() * 1E9)}${path.extname(file.originalname)}`;
cb(null, uniqueName)
},
})
let upload = multer({
storage,
limit: { fileSize: 1000000 * 100 },
}).single('myfile');
express.post('/', (req, res) => {
upload(res, req, async (error) => {
// validate request
if (!req.file) {
return res.json({ error: "Files are not validate :( " })
}
if (error) {
return res.status(500).send({ error: error.message })
}
// store in to databse
const file = new File({
filename: req.file.filename,
uuid: uuid4(),
path: req.file.path,
size: req.file.size
})
const response = await file.save();
return res.json({ file: `${process.env.APP_BASE_URL}/files/${response.uuid}` })
//
})
})
module.exports = express;
this is my error :
Problem with your callback. Try to reorganize as below.
let upload = multer({
storage,
limit: { fileSize: 1000000 * 100 },
});
express.post("/", upload.single("myfile"), async (req, res, error) => {
// validate request
// codes
}