у меня есть код на node js
.........
app.post("/slider", jsonParser, function(request, response) {
if(!request.body) {
console.log("error")
}
else {
connection.query("SELECT * FROM slider", function(err, result, fields) {
response.json(result)
})
connection.end()
}})
...
и есть код на js
var num = 0;
var slider_num = JSON.stringify({nubmer_slide: 1});
var request = new XMLHttpRequest();
request.open("POST", "/slider", true);
request.setRequestHeader("Content-Type", "application/json");
request.addEventListener("load", function () {
var receivedUser = JSON.parse(request.response);
for(var i of receivedUser) {
num++;
var key = `p${num}`
mass[`${key}`] = i.slider_picture
}
})
request.send(slider_num);
при первом клики сробатывает код js и данные верно получились с базы данных, но при втором клике пишет в консоли
Error: Can't add new command when connection is in closed state
at Connection._addCommandClosedState (C:\Users\spench\Documents\cifp\node_modules\mysql2\lib\connection.js:137:17)
at Connection.end (C:\Users\spench\Documents\cifp\node_modules\mysql2\lib\connection.js:819:26)
at C:\Users\spench\Documents\cifp\server.js:48:13
at Layer.handle [as handle_request] (C:\Users\spench\Documents\cifp\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\spench\Documents\cifp\node_modules\express\lib\router\route.js:137:13)
at C:\Users\spench\Documents\cifp\node_modules\body-parser\lib\read.js:130:5
at invokeCallback (C:\Users\spench\Documents\cifp\node_modules\raw-body\index.js:224:16)
at done (C:\Users\spench\Documents\cifp\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\spench\Documents\cifp\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:327:22)