skip to Main Content

I’m trying to get the result of an exists query using typescript but I can’t get the ‘1’ or ‘0’ of the object that the query is returning.

code.ts

const rslt1 = query(`SELECT EXISTS(SELECT * FROM patients WHERE cc=?)`, [params.ccInput]);
const rslt2 = query(`SELECT EXISTS(SELECT * FROM doctors WHERE specialty=?)`, [params.specialtyInput]);
(async () => {
        const frslt1:object = await rslt1
        console.log(frslt1, Object.values(frslt1))
        console.log(await rslt2)
})()

In the console I just get this:

[{ "EXISTS(SELECT * FROM patients WHERE cc='6465465465')": 0 } ] [ { "EXISTS(SELECT * FROM patients WHERE cc='6465465465')": 0 } ]
[{ "EXISTS(SELECT * FROM doctors WHERE specialty='Medicina general')": 1 } ]

I need the result of the query to do an if later

2

Answers


  1. Chosen as BEST ANSWER

    How it worked for me was this:

    code.ts

    (async () => {
      const rslt1:any = await query(`SELECT EXISTS(SELECT * FROM patients WHERE cc=?)`, [params.ccInput]);
      const rslt2:any = await query(`SELECT EXISTS(SELECT * FROM doctors WHERE specialty=?)`, [params.specialtyInput]);
            
      const res1 = Object.values(rslt1[0])[0]
      const res2 = Object.values(rslt2[0])[0]
      console.log('result1:', res1, 'result2:', res2)        
    })();
    

    In console I get this:

    result1: 0 result2: 1
    

  2. I think you should try to put the query call into your async function directly.

    Try

    (async () => {
         const rslt1 = await query(`SELECT EXISTS(SELECT * FROM patients WHERE cc=?)`, [params.ccInput]);
         const rslt2 = await query(`SELECT EXISTS(SELECT * FROM doctors WHERE specialty=?)`, [params.specialtyInput]);
    
         const frslt1: object = rslt1
         console.log(frslt1, Object.values(frslt1))
         console.log(rslt2)
    })()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search