我有一个用于处理mysql连接的类,每次访问路径/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
请注意,他说已连接到数据库,但是为什么我无法进行查询?
建立连接的类:
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 } }
mos
答案 0 :(得分:3)
您的Conexao
类具有1个功能:connect()
,但没有query()
函数。