Search code examples
node.jsapiexpressvalidationmulter

Node/Expressjs: Stop Posting The User Image If user email already exist


I have made an API for the user registration and have validated it with a unique email and name. But the issue is the API runs perfect and don't post, new user, if the user has the same email or name but the middleware of the image runs but I want that image should upload when all validations clear. Here is the code.

/ Making The Storage using Multer 
 var storage = multer.diskStorage({
  destination: function (req, file, cb) { cb(null, './UserImages') },
   filename: function (req, file, cb) { cb(null, Date.now() + file.originalname) }
 })
const upload = multer({
    storage: storage
 })
 // Making The Storage using Multer 

 // Posting Users to the Db With Collection Name UserCluster
 Router.post('/RegisterUser',upload.single('UserImage'), (req, res,) => {

//1 Manual Api for testing Uniqueness of Data.
// RegisterUserModel.findOne({ $or: [{ Name: req.body.name }, { Email: req.body.email }] }).then(UserExist => {
//     console.log(UserExist);
//     if (UserExist) {
//         if (UserExist.Email==req.body.email) {
//             res.json({
//                 Message: 'User Already Exists With This Email',
//                 Data: null
//             });
//         }
//         else if(UserExist.Name==req.body.name){
//             res.json({
//                 Message: 'User Already Exists With This UserName',
//                 Data: null
//             });
//         }
//         else{
//             res.json({
//                 Message: 'User Already Exists With This UserName and Email',
//                 Data: null
//             });
//         }
//     }
//     else {
//         const _RegisterUser = new RegisterUserModel({
//             Name: req.body.name,
//             Email: req.body.email,
//             Mobile: req.body.mobile,
//             Password: req.body.password,
//             ImageUrl: req.file.filename,
//             ImageName: req.file.originalname,
//             ImageMimeType: req.file.mimetype,
//             Address: req.body.address
//         });
//         _RegisterUser
//             .save()
//             .then(result => {
//                 res.json({
//                     Message: "User Register Successfully",
//                     Data: result
//                 })
//             })
//             .catch(err => {
//                 res.json({
//                     Error: err.message,

//                 })
//             });
//     }
// })

const _RegisterUser = new UserCluster({
    Name: req.body.name,
    Email: req.body.email,
    Mobile: req.body.mobile,
    Password: req.body.password,
    ImageUrl: req.file.filename,
    ImageName: req.file.originalname,
    ImageMimeType: req.file.mimetype,
    Address: req.body.address
});
_RegisterUser
    .save()
    .then(result => {
        res.json({
            Message: "User Register Successfully",
            Data: result
        })
    })
    .catch(err => {
        res.json({
            Error: err.message,
            ErrorObject:err,
            Data:null,
            

        })
    });

});

Simple: My question is that the image should upload when all validation is clear. if not the image should not be uploaded. Please help me Thanks.


Solution

  • I have tried it but as i am executing this code

            const _RegisterUser = new UserCluster({
                Name: req.body.name,
                Email: req.body.email,
                Mobile: req.body.mobile,
                Password: req.body.password,
                ImageUrl: req.file.filename,
                ImageName: req.file.originalname,
                ImageMimeType: req.file.mimetype,
                Address: req.body.address
             });
    

    i got the error that ERROR:Cannot read property 'filename' and it will come because multer middler ware execeutes later