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

172
Vistas
Get cypress database query output objects in to variables

I have a cypress test which has been set up with mysql node module. When I run bellow mentioned test Its giving output as follows.

const executeQuery = (query) => {
    cy.task('DBQuery', query).then(function (recordset) {
        var rec = recordset
        cy.log(rec)
    })
}

Query:

select * 
from Users 
where email = 'sheeranlymited@lymitedtest.com'

OUTPUT: log [Object{23}]

Query:

select firstname 
from Users 
where email = 'sheeranlymited@lymitedtest.com'

OUTPUT: log [{firstname: Edward}]

instead of cy.log(rec) I want to get the output of 23 columns to assign in to different variables based on the column name.

enter image description here

Appreciate if someone can help me to resolve this...

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use Object.values in js to retrieve values from your object

Let's say you need to extract the value of the 3rd column, so your code will look like,

cy.task('DBQuery', query).then(function (recordset) {
    var rec = recordset
    const results = Object.values(rec[0])
    // results[index of the column] will output the results
    cy.log(results[3])
})

We can do a small modification to make your task easier,

cy.task('DBQuery', query).then(function (recordset) {
    var rec = recordset
    const Values = Object.values(rec[0]);
    const keys = Object.keys(rec[0]);
    let result = {};
    let index = 0;
    keys.forEach(key => {
        result[keys[index]] = Values[index];
        i++
    })
     //result.firstName will give you your results
     cy.log(result.firstName);
})

In this way, we are generating key-value pairs having the key as the column name. So you can use the column name to find the value.

Hope this helps.

cheers.

over 4 years ago · Santiago Trujillo 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