大锅们好
我现在在做一个表单数据提交,但是状态码报了400,我估计是因为有图片数据上传原因,但是想不出是哪里出的问题
前端代码:
data:{
// 提交的数据
formData:{
src:'',
stylename:"",
typename:"",
showtype:'false',
discribe:"",
time:moment(new Date()).format(),
goodsname:"",
goodstype:"",
lablename:"",
price:0,
newprice:0,
title:""
},
// 提交事件
submitForm(formName) {
this.$refs[formName].validate(async(valid) => {
if (valid) {
if(!this.formData.src) {
this.$message.error('请选择图片上传')
return
}
const data = await postSwiper({url:'alldata',data:this.formData})
console.log(data);
} else {
console.log('error submit!!');
return false;
}
});
},
}
nodejs:
const express = require('express')
const multer = require('multer')
const router = express.Router()
const pool = require('../model/index')
const moment = require('moment')
let date = new Date()
let times = date.getTime()
let originalname;
let random = Math.floor(Math.random() * (1000 - 100 + 1)) + 100
let urlStr;
let obj;
let arr = [];
let src = ''
let storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'routers/img');
},
filename: function(req, file, cb) {
originalname = Buffer.from(file.originalname, "latin1").toString("utf8");
urlStr = times + '-' + random + '-' + originalname
times = date.getTime()
random = Math.floor(Math.random() * (10000 - 100 + 1)) + 100
src = 'http://localhost:3000/' + urlStr
obj = {
src,
stylename:"产品风格,如:简约风",
typename:"",
showtype:'false',
discribe:"备注",
time:moment(new Date()).format(),
goodsname:"商品名",
goodstype:"产品分类:床垫",
lablename:"",
price:1000,
newprice:0,
title:'请输入商品标题'
}
arr.push(obj)
cb(null, urlStr)
}
})
let upload = multer({ storage: storage });
// 增加商品数据 PC
router.post('/',upload.array('avatar',3),(req,res) => {
try {
if(req.file) {
pool.getConnection(function(err, connection) {
if (err) return console.log(err);
connection.query('insert into alldata values(0,?,?,?,?,?,?,?,?,?,?,?)',Object.values(arr[0]), function (error, results, fields) {
console.log('results',results);
connection.release();
if (error) return console.log(error);
});
})
arr = []
} else if(req.files) {
arr.forEach(val => {
pool.getConnection(function(err, connection) {
if (err) return console.log(err);
connection.query('insert into alldata values(0,?,?,?,?,?,?,?,?,?,?,?)',Object.values(val), function (error, results, fields) {
console.log('results',results);
connection.release();
if (error) return console.log(error);
});
})
})
arr = []
} else {
res.status(400).send(JSON.stringify({success: '服务器未接收到传递的数据或数据、格式错误'}))
return
}
res.send({code:200,msg:"成功"})
} catch (err) {
res.status(500).send(err);
}
})
module.exports = router
问题出在哪了啊大锅们,求解答