0

Everything is working as expected in my code except if the user has not uploaded a file it crashes my node app.

The error is arising at the fs.rename part, as there is nothing uploaded the app cant rename anything and causes the following:

Error:

EPERM: operation not permitted, rename 'C:\Users\xxx\AppData\Local\Temp\upload_0a145049089fa69e9df64f8d20abb362' -> 'C:\Users\xxx\Dropbox\Automate NodeJS\Login_Register+Submit - 0.01\data\5be13231807fe33f14b2834a\sdS9m'

Im having difficulty searching for my err and how to handle that, If anyone can point me to some resources for how to handle formidable errors and how to stop the process of the user hasn't uploaded anything it would be fantastic.

It's not a production app or anything of the like its just me learning how to deal with multiple functions and a database at once.

router.post('/submit', userisSubbed, userhasTime, (req, res, next0) => {
var userId = req.user._id;
var username = req.user.email;
var isCompleted = 'No'
var jobNumber = generator.generate({
    length: 5,
    numbers: true
});
// Validate
const errors = req.validationErrors();

if (errors) {
    res.render('index', {
        errors: errors
    });}    

    else {
        var form = new formidable.IncomingForm();
        form.parse(req, function (err, fields, files) {
        var userPath = req.user._id
        var dir = './data/' + userPath + '/' + jobNumber + '/';
        if (!fs.existsSync(dir)){
            fs.mkdirSync(dir);

          }else
          {
              console.log("Directory already exist");
        }

        var oldpath = files.filetoupload.path;
        var newpath = dir + files.filetoupload.name;
        // copy the file to a new location
        fs.rename(oldpath, newpath, function (err) {
            if (err) throw err;
            console.log('renamed complete');
        const newJob = new Job({
            userId: userId,
            username: username,
            isCompleted: isCompleted,
            filepath: newpath,
            jobNumber: jobNumber,
        });
        Job.createJob(newJob, function(err, job){
            if (err) throw err;
            console.log(job);
        });
    req.flash('success_msg', 'Job Submitted...');
    res.redirect('/')
     });
  });
 }
});
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • https://stackoverflow.com/questions/39293636/npm-err-error-eperm-operation-not-permitted-rename – Suresh Prajapati Nov 12 '18 at 05:54
  • @SureshPrajapati At your current reputation score you can actually "vote to close as duplicate". So if you think something is a duplicate, then please just do so. Questions can always be reopened **IF** there is a compelling reason that it requires a different solution from the duplicate. I will say however that this question is not about **npm install**, so though there's a common "underlying cause" then it's not the same thing, and does not need random links in comments either. – Neil Lunn Nov 12 '18 at 06:01
  • @SureshPrajapati whilst the error is the same the cause is different, thanks for trying though – Phillip Lakis Nov 12 '18 at 06:15
  • @PhillipLakis did u try `npm cache clean --force` and `npm install -g npm@latest` ? – Suresh Prajapati Nov 12 '18 at 06:18
  • @SureshPrajapati my error is caused by my router.POST and the rename not happening, Did you read my entire post and not just the error? And yes i tried your npm but it doesnt solve the empty field of the router.post – Phillip Lakis Nov 12 '18 at 06:20

0 Answers0