Closed okeze-stephen closed 10 months ago
Hello there you are appending image as product_img from the frontend and accessing as req.files.file in backend so it will be undefined and it will throw an error while using it please fix it and it might work as expected
and if it fixes it please close the issue
I have tried many times design an application for uploading and saving images on database but the each time i tried, the image file is not moving to the server directory. please i need help on how to resolve it.
This is my index.js for my node and Express Backend const express = require('express'); const uploadedFile = require('express-fileupload'); const cors = require('cors');
const app = express(); const port = process.env.PORT || 5000;
app.use(express.json()); app.use(cors()); app.use(uploadedFile());
// Upload Endpoint Request app.post('/upload', (req, res) => { if(req.files === null) { return res.status(400).json({ msg: 'No file to upload'});
/* file.mv(
${__dirname}/client/public/uploads/${file.name}
, err => { if(err) { console.error(err); return res.status(500).send(err); }});
app.listen(port, ()=> console.log(
Server Running on PORT:${port}
));Here is my code for react frontend
import React from 'react'; import '../styles/admin_styles/bootstrap.min.css'; import '../styles/admin_styles/responsive.css'; import '../styles/admin_styles/bootstrap-select.css'; import '../styles/admin_styles/perfect-scrollbar.css'; import '../styles/main.css'; import '../styles/styles.css'; import {Row, Col, Form, Button } from 'react-bootstrap'; import { link, useNavigate } from 'react-router-dom'; import { useState, useEffect } from 'react'; import axios from 'axios';
const AddProducts = () => {
return (
) }
export default AddProducts;