I have been having this issue with react trying to upload file using formData. I keep getting
this error. Thank you for your help.
my setUp:
react-scripts: "^4.0.3", node 14, multer: "^1.4.3",
internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name TypeError [ERR_INVALID_ARG_TYPE]: The "e of type string. Received undefined"
I have deleted my node_modules and run npm install but the same, I tried updating my react-scripts to the latest version still the same
Here is my react code section
const handleSubmit = async (e) => {
e.preventDefault();
if (newFile) {
console.log(newFile); // the file data is available
try {
const name = Date.now() + newFile.name;
const data = new FormData();
data.append('file', newFile);
data.append('name', name);
await axios.post('/tickets/upload', data); // I have set {headers : {"Content-Type" : "multipart/form-data"}} still not working
} catch (err) {
console.log(err);
}
}
};
Here is the code that handles the upload. note: it is working fine with postman (I can upload successfully with postman).
router.post('/', async (req, res) => {
if (req.user) {
try {
const newTicket = new Ticket(req.body)
const ticket = await newTicket.save()
res.status(201).json(ticket)
} catch (err) {
res.status(500).json(err)
}
} else {
res.status(403).json('You are allowed to create a ticket')
}
})