I am attempting to get data from my MySQL databse but it returns
[
[ '_events', [Object: null prototype] {} ],
[ '_eventsCount', 0 ],
[ '_maxListeners', undefined ],
[ 'next', [Function: resultsetHeader] ],
[ 'sql', 'SELECT * FROM dutylogs ORDER BY time DESC;' ],
[ 'values', undefined ],
[
'_queryOptions',
{
rowsAsArray: false,
sql: 'SELECT * FROM dutylogs ORDER BY time DESC;',
values: undefined
}
],
[ 'namedPlaceholders', false ],
[ 'onResult', [Function (anonymous)] ],
[ 'timeout', undefined ],
[ 'queryTimeout', null ],
[ '_fieldCount', 0 ],
[ '_rowParser', null ],
[ '_fields', [] ],
[ '_rows', [] ],
[ '_receivedFieldsCount', 0 ],
[ '_resultIndex', 0 ],
[ '_localStream', null ],
[ '_unpipeStream', [Function (anonymous)] ],
[ '_streamFactory', undefined ],
[
'_connection',
Connection {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: [ConnectionConfig],
stream: [Socket],
_internalId: 0,
_commands: [Denque],
_command: [Query],
_paused: false,
_paused_packets: [Denque],
_statements: [LRUCache],
serverCapabilityFlags: 2181036030,
authorized: true,
sequenceId: 1,
compressedSequenceId: 0,
threadId: 4009,
_handshakePacket: [Handshake],
_fatalError: null,
_protocolError: null,
_outOfOrderPackets: [],
clientEncoding: 'utf8',
packetParser: [PacketParser],
serverEncoding: 'utf8',
connectTimeout: null,
connectionId: 4009,
[Symbol(kCapture)]: false
}
],
[
'options',
{
isServer: undefined,
stream: undefined,
host: 'REDACTED',
port: 3306,
localAddress: undefined,
socketPath: undefined,
user: 'REDACTED',
password: 'REDACTED',
passwordSha1: undefined,
database: 'REDACTED',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
decimalNumbers: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
enableKeepAlive: false,
keepAliveInitialDelay: 0,
timezone: 'local',
queryFormat: undefined,
pool: undefined,
ssl: false,
multipleStatements: false,
rowsAsArray: false,
namedPlaceholders: false,
nestTables: undefined,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 224,
compress: false,
authPlugins: undefined,
authSwitchHandler: undefined,
clientFlags: 11203535,
connectAttributes: undefined,
maxPreparedStatements: 16000,
sql: 'SELECT * FROM dutylogs ORDER BY time DESC;',
values: undefined
}
]
]
From just reading that I can tell it is a connection log, but it doesn't help me much as I am really looking for something like below
[
{
key: 7044,
name: '~y~Ten~g~Creator~r~[RIP]',
id: '518334475038359555',
dept: 'SAHP',
time: 33,
lastclockin: '2022/11/25 22:30:06'
},
{
key: 7042,
name: '~y~Ten~g~Creator~r~[RIP]',
id: '518334475038359555',
dept: 'STAFF',
time: 2,
lastclockin: '2022/11/25 21:49:30'
},
{
key: 7043,
name: '~y~Ten~g~Creator~r~[RIP]',
id: '518334475038359555',
dept: 'BCSO',
time: 1,
lastclockin: '2022/11/25 21:52:58'
}
]
I am using the EJS view engine to render this but I have the code divided into a router, this was mainly to keep my code clean, more jargen to meet stack overflow's requirements!
const express = require('express')
const config = require('../config.json')
const mysql = require('mysql2')
const router = express.Router();
let print = console.log
const sql = mysql.createConnection({
host: config.sql.host,
user: config.sql.user,
password: config.sql.pass,
database: config.sql.database,
port: config.sql.port
})
sql.connect()
router.get('/:deptName', (req, res) => {
let dept = req.params.deptName
// print(Object.entries(getUnits(dept)))
res.render('index', {
siteName : dept,
serverName : config.serverName,
navEnabled : config.NavEnabled,
NavBtns : config.NavBtns,
deptList : config.departments,
viewDept : true,
dept : dept,
units : Object.entries(getUnits(dept))
})
})
function getUnits (dept) {
let sqlQuery = `SELECT * FROM dutylogs ORDER BY time DESC;`
let unitGetter = sql.query(sqlQuery, (err, res, field)=>{
if (err) throw err
print(res)
return res
})
return unitGetter
}
module.exports = router
I managed to fix it by putting this on line 5
let units = [];
and setting a time-out before I render the index!
setTimeout(()=>{
res.render('index', {
siteName : dept,
serverName : config.serverName,
navEnabled : config.NavEnabled,
NavBtns : config.NavBtns,
deptList : config.departments,
viewDept : true,
dept : dept,
units : units
})
}, 10)
then afterwards I had changed the function to set the units value to its response
function getUnits (dept) {
let sqlQuery = `SELECT * FROM dutylogs WHERE dept="${dept}" ORDER BY time DESC;`
let unitGetter = sql.query(sqlQuery, (err, res, field)=>{
if (err) throw err
units = res
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.