gpt4 book ai didi

mysql - 联系 MySql 时 Node 连接错误?

转载 作者:行者123 更新时间:2023-11-29 19:13:01 27 4
gpt4 key购买 nike

我编写了一个连接器来从 mysql 获取数据,当 iam 在路由内运行 Mysql 连接器时,它显示错误,并且浏览器正在运行到无限循环。

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
//var mongoose = require('mongoose');

//Connect to Mongoose

//mongoose.connect('mongodg://localhost/malwares');
//var db = mongoose.connection()


//Connect to Mysql
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.150.94',
user : 'root',
password : 'negalkgkgkal',
database : 'dbnamehere'
});

connection.connect();

//connection.query('SELECT * from detection', function(err, rows, fields) {
//if (!err)
//console.log('The solution is: ', rows);
//else
// console.log('Error while performing Query.');
//});

//connection.end();
//Mysql connection ends

app.get('/' , function(req ,res){
res.send('Goto /api/malware for detection results');
});
app.get('/malwares' , function(req , res){
console.log('Hello');
connection.query('SELECT * from rest', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
});

connection.end()
app.listen(3000);
console.log('Listening on port 3000');

错误日志

Listening on port 3000
Hello
Error while performing Query.

此后浏览器也没有响应。

我什至尝试使用 Node 检查器,它给了我这样的错误protocol-enqueue-after-fatal-error-in-node-mysql

关于如何解决此错误有什么建议吗?

P.S::数据库存在,表也存在,为了错误调试的目的,我查询了另一个表,我在代码中注释了该表并且它成功运行。

错误:

{ Error: Cannot enqueue Query after invoking quit.
at Protocol._validateEnqueue (/var/www/Rest/node_modules/mysql/lib/protocol/Protocol.js:202:16)
at Protocol._enqueue (/var/www/Rest/node_modules/mysql/lib/protocol/Protocol.js:135:13)
at Connection.query (/var/www/Rest/node_modules/mysql/lib/Connection.js:208:25)
at /var/www/Rest/app.js:39:13
at Layer.handle [as handle_request] (/var/www/Rest/node_modules/express/lib/router/layer.js:95:5)
at next (/var/www/Rest/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/www/Rest/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/Rest/node_modules/express/lib/router/layer.js:95:5)
at /var/www/Rest/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/var/www/Rest/node_modules/express/lib/router/index.js:335:12) code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }

最佳答案

如果我没有记错的话,如果连接是在同一个文件中定义的(就像您一样),您不需要 connection.connect() 部分,您可以尝试注释掉该行,然后运行它。我知道,因此我遇到了一些问题

关于mysql - 联系 MySql 时 Node 连接错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42934644/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com