我正在为项目创建上传。所以我为我上传创建了两条路线。第一个是当用户注册部分上传图像时第二个是如果驾驶员添加车辆他可以添加车辆的图像。
问题是当我第一次运行一个函数时它按预期工作。但如果我在运行任何这些功能后运行其他路由器。文件替换为上次上载的文件名。所以请帮我解决一下
这是用户注册上传功能
if(req.files.file.name.split('.').pop() == 'jpg' ||
req.files.file.name.split('.').pop() == 'jpeg' ||
req.files.file.name.split('.').pop() == 'png') {
Uploads
.findOne({}, {}, { sort: { 'created_at' : -1 }},
function(err, last){
if(err) throw err;
if(!last) {
Uploads
.create({
filename: fileName + '.' + extension,
path: licenseImg
},
function(err, license){
if(err) throw err;
Driver
.create({
driver_id: user._id,
driving_license_copy: license._id
},
function(){
req.files.file.mv(licenseImg + req.files.file.name, function(err){
if(err) return res.status(500).send(err);
fs.rename(licenseImg + req.files.file.name, licenseImg + fileName + '.' + req.files.file.name.split('.').pop(), function(err){
if(err) throw err;
config.sendWithHash(
user.email,
'Verify Your account',
'Please click the link down below to verofy your account \n http://localhost:1200/user/verify/email/',
user.NIC + user.phone + user.email
);
res.send({
message: 'New user created, Please verify your email first',
});
})
})
});
});
}
else {
var filename = last.filename;
var renamedName = filename.split('_')[0] + "_" + parseInt(parseInt(filename.split('.')[0].substr(filename.split('.')[0].lastIndexOf('_')+1))+1);
console.log(filename);
console.log(renamedName);
Uploads
.create({
filename: renamedName + '.' + extension,
path: licenseImg
},
function(err, license){
if(err) throw err;
Driver
.create({
driver_id: user._id,
driving_license_copy: license._id
},
function(){
req.files.file.mv(licenseImg + req.files.file.name, function(err){
if(err) return res.status(500).send(err);
fs.rename(licenseImg + req.files.file.name, licenseImg + renamedName + '.' + req.files.file.name.split('.').pop(), function(err){
if(err) throw err;
config.sendWithHash(
user.email,
'Verify Your account',
'Please click the link down below to verofy your account \n http://localhost:1200/user/verify/email/',
user.NIC + user.phone + user.email
);
res.send({
message: 'New user created, Please verify your email first',
});
})
})
});
});
}
});
}
else {
res.send('Cannot upload that type of files');
}
这是其他上传功能在驱动程序添加车辆的地方执行
Uploads
.findOne()
.sort({ 'created_at' : -1})
.exec(function(err, last){
if(err) throw err;
var filename = last.filename;
var filePath = './public/uploads/vehicles/';
var extension = last.filename.split('.').pop();
var renamedName = filename.split('_')[0] + "_" + parseInt(parseInt(filename.split('.')[0].substr(filename.split('.')[0].lastIndexOf('_')+1))+1);
console.log(last);
console.log(filename);
console.log(renamedName);
Uploads
.create({
filename: renamedName + '.' + extension,
path: filePath
},
function(err, vehicleImg){
if(err) throw err;
Vehicle
.create({
driver_id: req.decoded._doc._id,
make: req.body.make,
model: req.body.model,
year: req.body.year,
registration_no: req.body.reg_no,
vehicle_img: vehicleImg._id
},
function(){
req.files.file.mv(filePath + req.files.file.name, function(err){
if(err) return res.status(500).send(err);
fs.rename(filePath + req.files.file.name, filePath + vehicleImg.filename, function(err){
if(err) throw err;
res.send({
message: 'Vehicle added',
});
});
});
});
});
});
所以这里是错误来自的所有代码。如果有人知道为什么会这样,请让我知道如何解决它
谢谢