PS C:\Users\Administrator\Desktop\note_text> node .\app.js
服务器开启
error,result,fields Error: Cannot enqueue Query after invoking quit.
at Protocol._validateEnqueue (C:\Users\Administrator\Desktop\note_text\node_modules\mysql\lib\protocol\Protocol.js:215:16)
at Protocol._enqueue (C:\Users\Administrator\Desktop\note_text\node_modules\mysql\lib\protocol\Protocol.js:138:13)
at Connection.query (C:\Users\Administrator\Desktop\note_text\node_modules\mysql\lib\Connection.js:198:25)
at C:\Users\Administrator\Desktop\note_text\app.js:22:14
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\note_text\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Administrator\Desktop\note_text\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Administrator\Desktop\note_text\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\note_text\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Administrator\Desktop\note_text\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Administrator\Desktop\note_text\node_modules\express\lib\router\index.js:335:12) {
code: 'PROTOCOL_ENQUEUE_AFTER_QUIT',
fatal: false
} undefined undefined
请求接口后直接报这个错误,无法获取到数据库中查询到的数据,因为我将关闭数据库连接的方法end()
直接写到了接口的外面,只要一请求接口就会立即报出这个问题。解决方式是:将end方法写到请求接口的里面,防止还没有查询数据库就已经将数据库关闭了,所以这个时候去查询数据库就会抛出问题
const express = require('express')
const mysql = require('mysql')
const app = express()
// 创建数据库连接
let connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'root',
database:'my_base'
})
// 打开链接
connection.connect();
app.get('/text_get',(request,response)=>{
connection.query('select * from user',(error,result,fields)=>{
console.log('error,result,fields',error,result,fields);
})
response.send('哈喽')
// 关闭链接
connection.end()
})
app.listen(4399,()=>{
console.log('服务器开启');
})