Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
Cannot get specific information from a database using SQL

it's my first time making two SQL requests in on 'connection.query' and something is definetly not right, first of all here is my code:

pool.getConnection(function (err, connection) {
    if (err) throw err;
    let sql = `SELECT * FROM custom_prefix WHERE guild_id = '12345678912345678'; SELECT * FROM cache_size`;
    connection.query(sql, async function (err, result) {
        if (err) throw err;
        console.log(result);
        connection.release();
    });
});

So far everything works perfectly fine and the output will be:

[
  [
    RowDataPacket {
      prefix: '!',
      guild_id: 12345678912345678,
      guild_name: 'server name',
      changed_by_id: 12345678912345678,
      changed_by_name: 'username#1234',
      changed_date: 2022-04-13T19:50:33.000Z
    }
  ],
  [ RowDataPacket { guilds: 4, users: 39 } ]
]

If I change the console.log(result) to console.log(result[1]) the output will be:

[ RowDataPacket { guilds: 4, users: 39 } ]

So everything works like I would expect it, but if I only want a specific value from the result[1], lets say the guilds, then I would do console.log(result[1].guilds).

I would to this because I have some other files where I check the database for something and use the result[0].something method and there it works absolutely fine. But as soon as I use two SELECT * FROM requests it doesn't.

So if I try to console.log the result[1].guilds the output will be:

undefined

Which makes no sense for me because I oftenly use this method and it works fine. But here it says undefined.

So I don't know how to get a specific value from one of the results when using two , I hope somebody here can help me. Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Normally, a query result is an array of rows. However, since you performed multiple queries, result is an array of results of your queries; and each result is an array of rows returned by each query. Thus, you have one more level of nesting than in the scenario where you only have one query.

You are trying to get .guilds of an array, which does not have such a property. You want this instead:

result[1][0].guilds
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda