我正在创建Web服务以发送带有一些文本的图像。我从postman发送文件然后得到此错误未处理拒绝错误:发送后无法设置标题,req.body为空& req.files undefined。这是邮差的屏幕截图。 `
控制器代码在这里,上传&验证图像&一些文字数据。
var multer = require('multer');
var storage = multer.diskStorage({
destination : function(req,file,cb){
cb(null,'public/uploads/profile/');
},
filename : function(req,file,cb){
if(!file.originalname.match(/\.(jpg)$/)){
var err = new Error();
err.code = 'filetype';
return cb(err);
}else{
cb(null,file.originalname + '_' + Date.now());
}
}
});
var upload = multer({
storage : storage,
limits : {fileSize : 10000000}
}).single('myFile');
userController.saveUser = function(req,res){
upload(req,res,function(err){
if(err){
if(err.code === 'LIMIT_FILE_SIZE'){
return res.json({success:false,message:'File sizee is too large. Max limit is 10mb'});
}else if(err.code === 'filetype'){
return res.json({success:false,message:'File type is invalid.'});
}else{
console.log(err);
return res.json({success:'false',message:'File was not able to uploaded'});
}
}else{
if(!req.files){
return res.json({success:false,message:'No file selected!'});
}else{
return res.json({success:true,message:'File was uploaded successfully!'});
}
}
});
var params = req.body;
user.findOrCreate({
where:{
first_name: helper.trimInputValue(params.first_name),
email: helper.trimInputValue(params.email),
boltt_code: helper.trimInputValue(params.boltt_code)
},
defaults:{
first_name: helper.trimInputValue(params.first_name),
middle_name : '',
last_name : '',
email: helper.trimInputValue(params.email),
boltt_code: helper.trimInputValue(params.boltt_code),
is_active : helper.trimInputValue(user.INACTIVE),
}
})
.spread( function(user, created) {
var msg = '';
if(created){
msg = 'User inserted successfully!';
}else{
msg = 'User already exist!';
}
res.send({
message : msg,
status:'Success',
response:200,
data : user
});
}).catch(Sequelize.ValidationError,function(err){
data = err.errors;
var userError = Array();
if(data.length){
for(var key in data){
userError.push({
'key' : data[key].path,
'message' : data[key].message
});
}
}
res.send({
message : userError,
status : 'Failure',
response:200,
data:[]
});
}).catch((err) => {
res.send({
message : err,
status:'Failure',
response : 500,
data:[]
});
});
};
答案 0 :(得分:0)
我已经解决了我的问题,我需要在上传成功块中编写插入代码。
userController.saveUser = function(req,res){
upload(req,res,function(err){
if(err){
if(err.code === 'LIMIT_FILE_SIZE'){
res.send({success:false,message:'File sizee is too large. Max limit is 10mb'});
}else if(err.code === 'filetype'){
res.send({success:false,message:'File type is invalid.'});
}else{
console.log(err);
res.send({success:'false',message:'File was not able to uploaded'});
}
}else{
if(!req.files){
res.send({success:false,message:'No file selected!'});
}else{
var params = req.body;
console.log(params);
user.findOrCreate({
where:{
first_name: helper.trimInputValue(params.first_name),
email: helper.trimInputValue(params.email),
boltt_code: helper.trimInputValue(params.boltt_code)
},
defaults:{
first_name: helper.trimInputValue(params.first_name),
middle_name : '',
last_name : '',
email: helper.trimInputValue(params.email),
boltt_code: helper.trimInputValue(params.boltt_code),
is_active : helper.trimInputValue(user.INACTIVE),
}
})
.spread( function(user, created) {
var msg = '';
if(created){
msg = 'User inserted successfully!';
var mailOptions = {
from : 'test@boltt.com',
to : 'rajeev.web@boltt.com',
subject : 'New User Registered!',
html : '<b>Hello</b>'+ ' ' + user.dataValues.first_name + ' welcome to boltt family.'
};
transporter.sendMail(mailOptions,function(err,info){
if(err){
console.log(err);
}
console.log('Message %s sent : %s',info.messageId , info.response);
});
}else{
msg = 'User already exist!';
}
res.send({
message : msg,
status:'Success',
response:200,
data : user
});
}).catch(Sequelize.ValidationError,function(err){
data = err.errors;
var userError = Array();
if(data.length){
for(var key in data){
userError.push({
'key' : data[key].path,
'message' : data[key].message
});
}
}
res.send({
message : userError,
status : 'Failure',
response:200,
data:[]
});
}).catch((err) => {
res.send({
message : err,
status:'Failure',
response : 500,
data:[]
});
});
}
}
});
验证码工作正常
var storage = multer.diskStorage({
destination : function(req,file,cb){
cb(null,'public/uploads/profile/');
},
filename : function(req,file,cb){
if(!file.originalname.match(/\.(png)$/)){
var err = new Error();
err.code = 'filetype';
return cb(err);
}else{
cb(null,file.originalname + '_' + Date.now());
}
}
});
var upload = multer({
storage : storage,
limits : {fileSize : 10000000}
}).single('image');
未处理拒绝错误:发送后无法设置标头,发生此错误是因为我两次返回json响应。此错误也会被删除。现在我的代码工作正常。