Reputation: 121
I copied the source code from the tutorial for this page so I must have something wrong with my file path somewhere. Here is the error I am getting
Error-> Error: ENOENT: no such file or directory, rename 'C:\Users\Amazo\AppData\Local\Temp\3OOis2VHktYYKr2gQCX97Y2X.jpg' -> 'c:\Users\Amazo\Nodejs\SocialMediaProject1\uploads\568ab3707d355a981c73d41dWed Jan 06 2016 08:26:35 GMT-0600 (Central Standard Time)207330aba4a36cea73e8ed6fda9cb012.jpg'
Here is my code:
var User = require('../datasets/users');
var fs = require('fs-extra');
var path = require('path');
module.exports.updatePhoto = function (req, res){
var file = req.files.file;
var userId = req.body.userId;
console.log("User " + userId + " is submitting " , file);
var uploadDate = new Date();
var tempPath = file.path;
var targetPath = path.join(__dirname, "../../uploads/" + userId + uploadDate + file.name);
var savePath = "/uploads/" + userId + uploadDate + file.name;
fs.rename(tempPath, targetPath, function (err){
if (err){
console.log(err)
} else {
User.findById(userId, function(err, userData){
var user = userData;
user.image = savePath;
user.save(function(err){
if (err){
console.log("failed save")
res.json({status: 500})
} else {
console.log("save successful");
res.json({status: 200})
}
})
})
}
})
};
Upvotes: 0
Views: 1210
Reputation: 121
From much looking and research I have found that this error does not really tell you which path is actually wrong. So in this case I believe it to be the second path to be wrong as the first path is 100% true and exists.
What I did is in the:
var uploadDate = new Date();
I added:
uploadDate = uploadDate.toString;
This made it work for me. Not to sure why to be a 100% honest , but it must have something to do with how the rename function works with the fs node module. Either way if anyone else has feedback let me know what you find.
Upvotes: 1