[英]Cannot connect to Mysql database on Node JS
I have a Class that handle a mysql connection, and every time that I access de path /cargos
it should bring data, and close the connection. 我有一个处理mysql连接的类,每次访问de
/cargos
path /cargos
它都应该带数据并关闭连接。 The problem is that everytime when I try to Query, it throws me this menssage: 问题是,每次我尝试查询时,都会抛出此提示:
TypeError: conexao.query is not a function
at Cargos.getCargos (C:\work\PManager\api\src\models\cargos.model.js:8:17)
at C:\work\PManager\api\src\routes\cargos.js:8:16
at Layer.handle [as handle_request] (C:\work\PManager\api\node_modules\express\lib\router\layer.js:95:5)
at next (C:\work\PManager\api\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\work\PManager\api\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\work\PManager\api\node_modules\express\lib\router\layer.js:95:5)
at C:\work\PManager\api\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\work\PManager\api\node_modules\express\lib\router\index.js:335:12)
at next (C:\work\PManager\api\node_modules\express\lib\router\index.js:275:10)
at expressInit (C:\work\PManager\api\node_modules\express\lib\middleware\init.js:40:5)
Connected
Note that he says that connected to the database, but Why I Cannot make queries? 请注意,他说已连接到数据库,但是为什么我无法进行查询?
Class that makes de Connection: 建立连接的类:
const mysql = require('mysql');
class Conexao {
constructor() {
this.connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'agimplant'
});
this.connect()
}
connect() {
this.connection.connect((err) => {
if (!!err) {
console.log("Não consegui me conectar, COD DE ERRO: ", err.code, ", MENSAGEM:", err.message)
} else {
console.log("Connected")
}
})
}
}
module.exports = { Conexao }
The class that will use the connection: 将使用连接的类:
class Cargos {
constructor() {
}
getCargos(conexao, callback) {
console.log(conexao) // It shows me the properties of conexao
conexao.query('select * from cargos', callback)
}
}
module.exports = { Cargos }
And here is Where I Call the route: 这是我称之为路线的地方:
const { Cargos } = require('../models/cargos.model')
const { Conexao } = require('../config/conexao')
module.exports = function (app) {
app.get('/cargos', function (req, res) {
var conexao = new Conexao()
const cargos = new Cargos();
cargos.getCargos(conexao, (err, result) => {
if (err) {
res.json(err)
} else {
res.json(result)
}
})
});
}
When I make console.log(conexao) I shows me that: 当我制作console.log(conexao)时,我看到了:
Conexao {
connection:
Connection {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
config:
ConnectionConfig {
host: 'localhost',
port: 3306,
localAddress: undefined,
socketPath: undefined,
user: 'root',
password: undefined,
database: 'agimplant',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
ssl: false,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 455631 },
_socket: undefined,
_protocol:
Protocol {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
readable: true,
writable: true,
_config: [Object],
_connection: [Circular],
_callback: null,
_fatalError: null,
_quitSequence: null,
_handshake: false,
_handshaked: false,
_ended: false,
_destroyed: false,
_queue: [],
_handshakeInitializationPacket: null,
_parser: [Object] },
_connectCalled: false,
state: 'disconnected',
threadId: null } }
The mos 莫斯
您的Conexao
类具有1个函数: connect()
,但没有query()
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.