1

I have been trying to create an API in nestJS using just plain expressJS and mysql without using typeorm. However, there's little to no information for this either in the documentation or other sources.

What I have done so far:

  1. Created a global variable for holding connection received from mysql.createConnection().
  2. Tried to use this connection to get values from db:
async findAll() {
    return await connection.query('SELECT * from test', (error, results, fields) => {
        console.log(results);
        return results;
    });
}

The console is printing this data:

[ RowDataPacket {
    id: 1,
    firstName: 'test',
    middleName: '1',
    lastName: 'user' },
  RowDataPacket {
    id: 2,
    firstName: 'test',
    middleName: '2',
    lastName: 'user' }
]

However it is also throwing this error:

TypeError: Converting circular structure to JSON 
at JSON.stringify (<anonymous>)

EDIT:

I tried this solution from the link @Kim Kern suggested:

return await connection.query('SELECT * from users', (error, results, fields) => {
    results = results.map((result) => {
        return Object.assign({}, result);
    });
    console.log(results);
    return results;
});

Now the results do not have RowDataPacket, but still throw the same error.

nik7
  • 806
  • 3
  • 12
  • 20
Sanju
  • 1,478
  • 2
  • 20
  • 41
  • 1
    The problem seems to be `RowDataPacket`. Have a look at this thread, it proposes a hacky solution: https://stackoverflow.com/a/34460319/4694994 – Kim Kern Mar 11 '19 at 15:45
  • 1
    Also, have a look at the docs that use typeorm without the `@nestjs/typeorm` package. I think you can reuse a lot of the structures and just swap typeorm with mysql: https://docs.nestjs.com/recipes/sql-typeorm – Kim Kern Mar 11 '19 at 15:47

2 Answers2

1

Finally !! after digging for hours I got it. The problem is with the callback, it's not returning the desired Promise. It's Returning an Unparsable JSON.

Basically, What You have to do is.

async getAll(){
 const res = await this.execExec(connection)
 return res;
}

execExec(connection){
 return new Promise((res, rej) => {
    connection.query('SELECT * from users', function (error,results,fields) {
      if (error) throw error;
      results = results.map((result) => {
         return Object.assign({}, result);
    });
      console.log(results);
      res(results);
    });
  });
}
1
const mysql = require('mysql2');
export class TestService {
async selectAll(): Promise<any> {
        let connection = mysql.createPool({
            host: 'localhost',
            user: 'root',
            password: 'root',
            database: 'testDB'
        });
        return new Promise((res, rej) => {
            connection.execute("select * from test", (e, results) => {
                if (e) throw e;
                //  console.log(results);
                res(results);
            });
        })
    }
}
  • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. – nik7 Apr 19 '21 at 09:37