Why posting the request directly returns a invalid argument error but saving the request to the filesystem first and then posting it doesn't?
im trying to post a file from an API request but it returns an "ERR_INVALID_ARG_TYPE" message. my implementation is like
const file= req.files.image
cloudinary.v2.uploader.upload(
file,
{exif: true, public_id: `images/${file.name}${id}`},
function(err, response){
if(err) res.send(err)
res.status(200).json({
message: response,
id: `${file.name}${id}`
})
});
but when I save the image from the request to the file system first, it works
const file= req.files.image
const uploadPath= `${__dirname}/../uploads/${file.name}`
file.mv(uploadPath, function(err){
if (err){
return res.status(500).send(err)
}
})
cloudinary.v2.uploader.upload(
uploadPath,
{exif: true, public_id: `images/${file.name}${id}`},
function(err, response){
if(err) res.send(err)
res.status(200).json({
message: response,
id: `${file.name}${id}`
})
fs.unlinkSync(uploadPath)
});
I hope someone could help me
Thank You
-
Hi Duta,
Thank you for reaching out.
In your first code snippet you are not extracting the temporary file path so try again with this:
const file= req.files.image.path
When you just request `req.files.image`, you get the json response with the details of the file and uploading that results in `ERR_INVALID_ARG_TYPE`.
Please let me know if you have any other questions or queries.
Kind Regards,
Thomas
Please sign in to leave a comment.
Comments
1 comment