Backend File :
const express = require("express");
const router = express.Router();
const fetchUser = require("../../middleware/fetch_User")
const Faculty = require("../../model/Faculty_Detail");
const fs = require('fs');
const path = require('path');
const multer = require("multer");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log('start')
cb(null, '../admin/Faculty/FacultyImages');
console.log('end')
},
filename: function (req, file, cb) {
cb(null, Date.now() + file.originalname);
}
})
const upload = multer({ storage: storage });
// /api/admin/Faculty_Member_Update
router.put("/Faculty_Member_Update/:id", upload.array('image',2), async (req, res) => {
try {
const { FullName, Qualification, Branch, Phone_No, Address, Aadhar_no, Staff_type, Designation, Gender, Experience, DateOfBirth} = req.body
let success = false;
console.log(req.file)
const user = await Faculty.findById(req.params.id);
if (user && user.Image) {
const imagePath = path.join(__dirname, '..', '..', '..', '..', 'src', 'component', 'admin', 'Faculty', 'FacultyImages', user.Image);
fs.unlinkSync(imagePath);
}
const UpdatedList = {};
if (FullName) { UpdatedList.FullName = FullName };
if (Qualification) { UpdatedList.Qualification = Qualification };
if (Branch) { UpdatedList.Branch = Branch };
if (Phone_No) { UpdatedList.Phone_No = Phone_No };
if (Address) { UpdatedList.Address = Address };
if (Staff_type) { UpdatedList.Staff_type = Staff_type };
if (Experience) { UpdatedList.Experience = Experience };
if (Designation) { UpdatedList.Designation = Designation };
if (Gender) { UpdatedList.Gender = Gender };
if (Aadhar_no) { UpdatedList.Aadhar_no = Aadhar_no };
if (DateOfBirth) { UpdatedList.DateOfBirth = DateOfBirth };
if (req.file) { UpdatedList.Image = req.file.filename };
const IsUser = await Faculty.findById(req.params.id);
if (!IsUser) {
return res.status(404).json({ success, message: "Not Found" })
}
if (IsUser._id.toString() !== req.params.id) {
return res.status(401).json({ message: "Not Allowed" });
}
const Faculty_list = await Faculty.findByIdAndUpdate(req.params.id, { $set: UpdatedList }, { new: true });
success = true
res.json({ success, message: "updated Successfully" });
} catch (error) {
console.log(error);
}
})
module.exports = router
Frontend file:
<div className="input-control-group-image d-flex" style={{ flexDirection: 'column' }}>
<div className="input-control-image d-flex fix_h_w" style={{ width: '16rem', height: "15rem", border: '0.1px solid grey' }}>
{imagefile !== null ?
<img src={imagefile} alt="" style={{ padding: '1rem' }} /> :
<img src={image === null ? userprofile : require(`./FacultyImages/${image}`) || imagefile} alt="" style={{ padding: '1rem' }} />}
</div>
<div className="input-control d-flex" style={{ width: ' 100%' }}>
<label className="label" style={{ width: ' 12rem' }} htmlFor="">Update Profile</label>
<input type="file" className="" id="file" name='image' accept='jpeg,png' onChange={handleFileChange} />
</div>
</div>
const multer = require("multer");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log('start')
cb(null, '../admin/Faculty/FacultyImages');
console.log('end')
},
filename: function (req, file, cb) {
cb(null, Date.now() + file.originalname);
}
})
this block will execute only when we add image but not in image update Why this is happening.
when i try to update a image its not working but when i try to add image this will work. when check it with console.log(req.file) it will show undefined
i try in another file but it show same issue why it does not take image from input feild when all are good