У меня есть класс, который обрабатывает соединение mysql, и каждый раз, когда я обращаюсь к de path /cargos
, он должен приносить данные и закрывать соединение.Проблема в том, что каждый раз, когда я пытаюсь выполнить запрос, он выдает мне следующее сообщение:
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
Обратите внимание, что он говорит, что подключен к базе данных, но почему я не могу делать запросы?
Классэто делает де 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 }
Класс, который будет использовать соединение:
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 }
А вот где я вызываю маршрут:
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)
}
})
});
}
КогдаЯ делаю 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 } }
Мос