Estoy tratando de trabajar con una base de datos en node.js. Tengo una función asíncrona para verificar y ver si existe un registro en la base de datos. Entonces la función debería devolver verdadero o falso.
Actualmente, la función regresa undefined y en los registros veo que las líneas de console.log que están en esa función se muestran después de lo que debería ser el valor devuelto por la función asíncrona.
Aquí está mi código
const PostgresCheckExist = async (id) => { try { console.log(`-=STARTING POSTGRES CHECK EXISTS=-`); client.query(`SELECT token from oauth where id = '${id}';`, (err, res) => { console.log(res.rowCount); const rowCount = res.rowCount; if (err) throw err; if (rowCount <= 0) { console.log(`The row count was ${rowCount}!\nEntered the <= if marker`); client.end(); console.log(`Made is passed the 'client.end()' call!`); return false; } else if (rowCount >= 2) { client.end(); console.log(`The row count was ${rowCount}!\nEntered the >= if marker`); throw "ERROR: More than one item returned on for Primary Key. Please check database"; } else { console.log(`The row count was ${rowCount}!\nEntered the else marker`); console.log(rowCount); client.end(); return true; } }); } catch (err) { console.error(err); } }; v1.get("", async (req, res) => { const respone = await PostgresCheckExist(1) console.log(response); res.json({ success: false }); });Esto se llama desde un enrutador express.
También aquí está la salida del comando.
app[web.1]: -=STARTING POSTGRES CHECK EXISTS=- app[web.1]: undefined app[web.1]: 0 app[web.1]: The row count was 0! app[web.1]: Entered the <= if marker app[web.1]: Made is passed the 'client.end()' call!