Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!