联系MySql时出现节点连接错误?

时间:2017-03-21 17:51:34

标签: mysql node.js mongodb express

我编写了一个连接器来从mysql获取数据,当我在路径中运行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-inspector,它给了我这样的错误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 }

1 个答案:

答案 0 :(得分:0)

如果我没有弄错,你不需要connection.connect()部分如果连接在同一个文件中定义(就像你有的那样),你试着注释掉那一行然后再运行它。 我知道,因为那个我遇到了一些问题