Nodejs报Cannot enqueue Query after invoking quit错误的解决

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('服务器开启');
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值