哎呀,我这个菜袅又来啦
这个问题是nodejs代码应该是没问题的,post请求数据是能增加的,只是会时不时报错说:在已经对握手进行排队后,不能对握手进行排队,
nodejs代码:
const express = require('express')
const router = express.Router()
const multer = require('multer')
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 });
// 单条swiper数据增加
router.post('/odd',upload.single('avatar'),(req,res) => {
try {
console.log('file', req.file);
console.log('body', req.body);
if(req.file) {
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);
}
})
module.exports = router
报错:
Error: Cannot enqueue Handshake after already enqueuing a Handshake.
at Protocol._validateEnqueue (c:\Users\21276\Desktop\NodeJsSquare\node_modules\mysql\lib\protocol\Protocol.js:221:16)
at Protocol._enqueue (c:\Users\21276\Desktop\NodeJsSquare\node_modules\mysql\lib\protocol\Protocol.js:138:13)
at Protocol.handshake (c:\Users\21276\Desktop\NodeJsSquare\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (c:\Users\21276\Desktop\NodeJsSquare\node_modules\mysql\lib\Connection.js:116:18)
at c:\Users\21276\Desktop\NodeJsSquare\model\orm.js:216:20
at new Promise (<anonymous>)
at Model.connect (c:\Users\21276\Desktop\NodeJsSquare\model\orm.js:215:14)
at Model.insert (c:\Users\21276\Desktop\NodeJsSquare\model\orm.js:94:14)
at c:\Users\21276\Desktop\NodeJsSquare\routers\swiper.js:43:20
at Layer.handle [as handle_request] (c:\Users\21276\Desktop\NodeJsSquare\node_modules\express\lib\router\layer.js:95:5) {
code: 'PROTOCOL_ENQUEUE_HANDSHAKE_TWICE',
fatal: false
}
问题是出在哪啊,nodejs和数据库连接,我有用到orm,我有试过请求后关闭连接,但是没用,为啥呀,求大锅解答呀