I am stuck when i try to upload both single and an array of images using multer. The process is working with postman but delivering undefined req.file(s) when i post through React.
Below is code snippets
frontend product.js
class Products extends Component {
state = {
name: '',
quantity: '',
price: '',
description: '',
offer: '',
manufacturer: '',
expiryDate: null,
photos: [],
msg:'',
show:false,
approved:'',
reviews: '',
category: ''
}
static propTypes = {
loadProducts: PropTypes.func.isRequired,
saveProduct: PropTypes.func.isRequired,
error500: PropTypes.bool,
error: PropTypes.object.isRequired,
clearErrors: PropTypes.func.isRequired,
clearSuccess: PropTypes.func.isRequired,
returnSuccess: PropTypes.func.isRequired
}
componentDidMount() {
this.props.loadProducts()
this.props.clearErrors()
}
componentDidUpdate(prevProps) {
const { error} = this.props
if (error !== prevProps.error) {
//check for PRODUCT error
if (error.id === 'PRODUCT-ERROR') {
this.setState({ msg: error.msg.message })
} else {
this.setState({ msg: null })
}
}
}
handleChange = (e) => {
this.setState({ [e.target.name]: e.target.value })
}
handleEditorChange = (description, editor) => {
this.setState({'description': description});
}
handlePhotos =(e)=>{
this.setState({photos:e.target.files})
}
handleSubmit =(e) =>{
e.preventDefault();
const { name,quantity,price,description, offer,manufacturer,
expiryDate,photos,approved,reviews,category
}= this.state
const product ={name,quantity,price,description, offer,manufacturer,
expiryDate,photos,approved,reviews,category};
this.props.saveProduct(product);
this.props.clearErrors()
}
<Form onSubmit={this.handleSubmit} enctype="multipart/form-data">
<Form.File
onChange={this.handlePhotos}
accept="image/*"
multiple
name='photos'
type='file'
placeholder="Product photos" />
frontend productAction.js
export const saveProduct =({ name,quantity,price,description, offer,manufacturer,
expiryDate,photos,approved,reviews,category
}) =>async (dispatch) =>{
console.log(photos)
//Headers
const headers = {
'Content-Type':'multipart/form-data'
}
//Request Body
const data = { name,quantity,price,description, offer,manufacturer,
expiryDate,photos,approved,reviews,category
}
console.log(data)
await axios.post('http://127.0.0.1:2000/api/product/create',data,headers)
.then(res =>{
dispatch({
type: CREATE_PRODUCT,
payload: res.data,
})
dispatch(
returnSuccess(res.data,res.status,'PRODUCT-CREAT-SUCCESS')
)
})
***backend product.js ***
//initialize app
const app= express();
//initialize cors
app.use(cors());
//initialize bodyparser
app.use(express.urlencoded({extended:true}));
app.use(express.json());
const storage = multer.diskStorage({
filename: function (req, file, cb) {
cb(null, shortid.generate()+ '-' + file.originalname)
},
})
const uploads = multer({storage:storage,fileFilter: (req, file, cb) => {
if (file.mimetype == "image/png" || file.mimetype == "image/jpg" || file.mimetype == "image/jpeg") {
cb(null, true);
} else {
cb(null, false);
return cb(new Error('Only .png, .jpg and .jpeg format allowed!'));
}
}});
router.post('/product/create',uploads.array('photos',10),
async(req,res,next)=>{
//res.status('200').json({file:req.files, body: req.body})
let photos =[]
console.log(req.files)
Someone help please am stuck for days
question from:
https://stackoverflow.com/questions/65897351/req-files-and-req-files-are-defined