获取 TypeScript 中 exists 查询的结果

huangapple go评论71阅读模式
英文:

Get the result of exists query in typescript

问题

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

英文:

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

答案1

得分: 0

我认为你应该尝试将 query 调用直接放入你的异步函数中。

尝试:

(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)
})()
英文:

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)
})()

答案2

得分: 0

以下是您要翻译的内容:

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)        
})();

在控制台中我得到这个:

result1: 0 result2: 1
英文:

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

huangapple
  • 本文由 发表于 2023年5月11日 02:10:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定