Home > Back-end >  Error occurs on promise in node.js looking to make this a promised connection and fix
Error occurs on promise in node.js looking to make this a promised connection and fix

Time:11-15

Error: Callback function is not available with promise clients. at PromisePoolConnection.query (/var/www/html/node_modules/mysql2/promise.js:95:13) at Timeout._onTimeout (/var/www/html/index.js:358:16) at processTicksAndRejections (internal/process/task_queues.js:97:5)

setInterval( async function() {
const conn = await db.getConnection();
const checkBuffs = await conn.query("SELECT * FROM userBuffs WHERE user_id IS NULL AND expires < ?", [calculations.dateAdd(new Date(), 'hour', 4)]);
for (let i = 0; checkBuffs[0][i]; i  ) {
    io.emit('buff_delete', checkBuffs[0][i].buff_id);
    await conn.query("DELETE FROM userBuffs WHERE ID = ? LIMIT 1", [checkBuffs[0][i].ID]);
}
if (checkBuffs[0][0]) { cache.flushAll(); }
await conn.query("SELECT ID, idle_timer, action, combat FROM users WHERE idle_timer >= 1 ORDER BY ID ASC", async function(err, results) {
    await conn.release();
    if (err) { console.log(err); throw err; }
    for (let i = 0; i < results.length; i  ) {
        if (results[i].idle_timer <= 1) {
            if (results[i].action > 0) { actionStop(results[i].ID); update.user('action', '=', 0, results[i].ID); }
            if (results[i].combat > 0) { combatStop(results[i].ID); update.user('combat', '=', 0, results[i].ID); }
            update.user('idle_timer', '=', 0, results[i].ID);
        } else {
            update.user('idle_timer', '=', (results[i].idle_timer - 1), results[i].ID);
        }
    }
});}, 60000 );

error occurs at await conn.query("SELECT ID, idle_timer, action, combat FROM users WHERE idle_timer >= 1 ORDER BY ID ASC", async function(err, results) {

looking to convert this to a promise?

CodePudding user response:

You could use MySQL for fetching data

in callback mode

conn.query("SELECT ID, idle_timer, action, combat FROM users WHERE idle_timer >= 1 ORDER BY ID ASC", function(err, results) {...})

or in the Promise mode

conn.query("SELECT ID, idle_timer, action, combat FROM users WHERE idle_timer >= 1 ORDER BY ID ASC").then(results => { ... })

or maybe in Promise mode with Await/Async syntax sugar

const result = await conn.query("SELECT ID, idle_timer, action, combat FROM users WHERE idle_timer >= 1 ORDER BY ID ASC")
    //...
    const results = await conn.query("SELECT ID, idle_timer, action, combat FROM users WHERE idle_timer >= 1 ORDER BY ID ASC");
    await conn.release();

    for (let i = 0; i < results.length; i  ) {
        //... processing results
    }

if you new to async/await please read about promises, at least https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Statements/async_function

  • Related