nodejs:
const express = require('express')
const router = express.Router()
const multer = require('multer')
const pool = require('../model/index')
const mysql = require('../model/orm')
let date = new Date()
const moment = require('moment')
let time = date.getTime()
let originalname;
let random = Math.floor(Math.random() * (1000 - 100 + 1)) + 100
let urlStr;
let obj;
let arr = [];
let swi_url = ''
let storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'public/upload');
},
filename: function(req, file, cb) {
originalname = Buffer.from(file.originalname, "latin1").toString("utf8"); // 解决接收文件的文件名中文乱码问题
urlStr = time + '-' + random + '-' + originalname
time = date.getTime()
random = Math.floor(Math.random() * (10000 - 100 + 1)) + 100
swi_url = 'http://localhost:3000/upload/' + urlStr
obj = {
swi_url,
swi_qiyong:'false',
swi_type:"活动",
swi_time:moment(new Date()).format()
}
arr.push(obj)
cb(null, urlStr)
}
})
let upload = multer({ storage: storage });
// 查询数据
router.get('/',(req,res) => {
try{
let swiper = mysql.model('swiper')
swiper.find((err,data) => {
if(err) {
console.log(err);
res.send(err)
return
}
console.log(data);
res.send(data)
})
} catch (err) {
console.log(err);
}
})
router.post('/odd',upload.single('avatar'),(req,res) => {
try {
console.log('file', req.file);
console.log('body', req.body);
if(req.file) {
pool.getConnection(function(err, connection) {
if (err) console.log(err); // not connected!
connection.query('insert into swiper values(0,?,?,?,?)',Object.values(arr[0]), function (error, results, fields) {
console.log('results',results);
arr = []
connection.release();
if (error) console.log(error);;
});
})
// console.log(arr);
// let swiper = mysql.model('swiper')
// swiper.insert(arr[0],(err,data) => {
// if(err) {
// res.send(err)
// return
// }
// res.status(200).send(JSON.stringify({success: '数据添加成功',code:200,swi_url:swi_url}))
// })
} else {
res.status(400).send(JSON.stringify({success: '服务器未接收到传递的数据或数据、格式错误'}))
}
} catch (err) {
if (err instanceof multer.MulterError) {
console.log(err.code);
} else {
console.log(err);
}
res.status(500).send(err);
}
})
// 修改数据
router.put('/:id',(req,res) => {
try {
console.log('put----请求',req.body);
let swiper = mysql.model('swiper')
let id = req.body.id
let obj = {
swi_url: req.body.swi_url,
swi_type: req.body.swi_type,
swi_qiyong: req.body.swi_qiyong,
swi_time: req.body.swi_time
}
swiper.update(`id=${id}`,obj,(err,data) => {
if(err) {
console.log(err);
res.send(err)
return
}
res.send({code:200,msg:'数据修改成功!'})
})
} catch (err) {
console.log(err);
res.send(err)
}
})
module.exports = router
前端报错:
这是为啥呀,明明post请求成功了,数据库数据也增加了,还是会有这个报错,求大锅解答啊