# Typescript select query

**URL:** <https://forum.codewithmosh.com/t/typescript-select-query/14416>\
**Category:** TypeScript\
**Created:** [August 15, 2022, 9:04pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416 "2022-08-15T21:04:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![franciscop](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@franciscop](https://forum.codewithmosh.com/u/franciscop)\
**Post date:** [August 15, 2022, 9:04pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416/1 "2022-08-15T21:04:44Z")

</div>

Hello Everyone,

I am new to Node/Typescript and I am trying to create a simple query but I am having some issues on returning the result of my query.

Here’s my code:

```auto
 app.get("/info", async (req: Request, res: Response) => {
     const obj = new UserService;
     const result = await obj.getUserbyEmail();
     res.json({
         "status" : status_code.success,
         "payload" : {
             response: result
         }
     });
 });

 export default class UserService{
     async getUserbyEmail(){  
         const result = await dbconnect.query("SELECT * FROM users WHERE email = 'email@gmail.com'");
         return result;
     }
 }

```

I keep having an error.  
(node:21380) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON  
 → starting at object with constructor ‘Query’

I would like to ask for some of your expert help.

Thank you!

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 15, 2022, 9:27pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416/2 "2022-08-15T21:27:30Z")

</div>

So I am gathering that the object returned by `dbconnect.query` returns an object which cannot be represented in JSON since it has a circular reference. What sort of object might have a circular reference? In the most trivial case, this one:

```auto
class Oroboros {
  public Oroboros self;
  constructor() {
    this.self = this;
  }
}

```

Anything with a circular reference cannot be converted to JSON.

You did not give me enough information here to see what `dbconnect.query` is returning, but I am betting that what it returns has a circular reference so you may need to grab just part of that object to convert it to JSON… Also, please use code blocks to enclose code samples to make it easier to read (basically wrap them in three backticks: ```)

---

<div class="post-metadata">

**Author:** ![franciscop](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@franciscop](https://forum.codewithmosh.com/u/franciscop)\
**Post date:** [August 15, 2022, 9:50pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416/3 "2022-08-15T21:50:34Z")

</div>

Thanks for your response. I am just returning a simple JSON with users information.

Sample

> | status | 201 |
> | --- | --- |
> | payload | |
> | response | |
> | 0 | |
> | userid | 2 |
> | usertoken | 54125960-bc47-450c-afb3-daab9d77d001 |
> | email | [email@gmail.com](mailto:email@gmail.com) |
> | first\_name | fname |
> | is\_active | 1 |
> | last\_name | lastname |
> | password | password\_field |

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 15, 2022, 10:26pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416/4 "2022-08-15T22:26:42Z")

</div>

Sorry, still not clear to me, what type of object is `dbconnect.query` returning at this line:

```auto
const result = await dbconnect.query("SELECT * FROM users WHERE email = 'email@gmail.com'");

```

---

<div class="post-metadata">

**Author:** ![franciscop](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@franciscop](https://forum.codewithmosh.com/u/franciscop)\
**Post date:** [August 15, 2022, 10:48pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416/5 "2022-08-15T22:48:07Z")

</div>

Hi, sorry I wasn’t clear about it. Okay it’s coming from a db connection.

Here’s the code below

```auto
export let mysql = require('mysql');
export let dbconnect = mysql.createConnection(dbconfig);
dbconnect.connect();

```

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 15, 2022, 11:04pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416/6 "2022-08-15T23:04:35Z")

</div>

So early versions of mysql had at least one circular reference inside the Query object: [mysql/query.js at a10d0251b34ea73336d59652824a6a4c6d4f8407 · mysqljs/mysql · GitHub](https://github.com/mysqljs/mysql/blob/a10d0251b34ea73336d59652824a6a4c6d4f8407/lib/query.js#L7)

Due to that, I think you need to select the actual fields you wanted to select out of the properties on the Query object itself. I believe these should be the rows of the table, but you could log the object to the console to verify:

```javascript
const result = await dbconnect.query("SELECT * FROM users WHERE email = 'email@gmail.com'");
console.log("result =", result);
return result;

```

---

<div class="post-metadata">

**Author:** ![franciscop](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@franciscop](https://forum.codewithmosh.com/u/franciscop)\
**Post date:** [August 15, 2022, 11:26pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416/7 "2022-08-15T23:26:21Z")

</div>

I refactored the code into this

```auto
export const getAllUsers = async (req: Request, res: Response) => {
    await dbconnect.query('SELECT * FROM users', (error: Error, result: any) => {
        res.json({
                "status" : 201,
                "payload" : {
                    response: result
                }
            });
        }
    );
}

```

But I am still looking for an example where I can code the query like this (below).

```auto
const result = await dbconnect.query("SELECT * FROM users WHERE email = 'email@gmail.com'");

```

It might be the version of the library that I use so I’ll try other versions.

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 15, 2022, 11:49pm UTC](https://forum.codewithmosh.com/t/typescript-select-query/14416/8 "2022-08-15T23:49:00Z")

</div>

I think you have to use mysql at versions before 2.0.0 to get that to run. They require the callback after 2.0.0 as near as I can tell.
