哎呀,我这个菜袅又来啦
这个问题是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 ()
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,我有试过请求后关闭连接,但是没用,为啥呀,求大锅解答呀
基于Monster 组和GPT的调写:
错误是由于在已经排队等待握手的情况下再次尝试进行握手造成的。在这种情况下,可以尝试在调用insert方法之前确保ORM连接到数据库,并且在完成操作后释放连接。
在代码中,似乎没有直接调用ORM连接数据库的代码。根据提供的代码,我推测这个问题可能是在使用ORM连接数据库的代码中出现的。具体来说,可以检查orm.js中的连接代码,以查看是否存在连接问题。
此外,我还建议尝试使用连接池来管理ORM连接。这可以减少连接的创建和销毁次数,并提高应用程序的性能。
以下是一个使用连接池的示例:
const mysql = require('mysql');
const pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
// Use the connection
connection.query('SELECT something FROM sometable', function (error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});
例子中,getConnection方法从连接池中获取一个可用的连接,并在完成操作后通过调用connection.release()将其返回到池中。