Typescript select query

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:

 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!

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:

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: ```)

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
first_name fname
is_active 1
last_name lastname
password password_field

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

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

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

Here’s the code below

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

So early versions of mysql had at least one circular reference inside the Query object: mysql/query.js at a10d0251b34ea73336d59652824a6a4c6d4f8407 · mysqljs/mysql · GitHub

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:

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

I refactored the code into this

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).

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.

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.

1 Like