Home > Net >  How to make a query in the Postgress database asynchronously for testing in cypress?
How to make a query in the Postgress database asynchronously for testing in cypress?

Time:08-02

I am trying to get data from the database through an exported function. But the problem is that the function is returning empty data. The query works if executed outside the function, but when I put the query inside the function, the return is empty. I believe that the function is returning before the query gets the result from the database.

My code:

export function queryDb(query) {
    return new Promise(() => {
        database.select(database.raw(query).then(data => {
            return data;
        }).catch(err => {
            console.log(err.message);
        })
        )
    })
}

CodePudding user response:

I think you need resolve and reject in your function

export function queryDb(query) {
    return new Promise((resolve, reject) => {
        database.select(database.raw(query).then(data => {
            resolve(data);
        }).catch(err => {
            console.log(err.message);
            reject(err)
        })
        )
    })
}

If you turn that into a Now I'm getting this error from cypress

The solutions for the promise, worked in another project apart using just knex. But in cypress, this error is returned.

  • Related