My current project involves creating an API in nestJS
using only plain expressJS
and mysql
, without relying on typeorm
. Unfortunately, I've found very limited information in the documentation or other sources to guide me through this process.
Here's what I've completed so far:
- Established a global variable to store the connection obtained from
mysql.createConnection()
. - Attempted to utilize this connection to retrieve data from the database:
async findAll() {
return await connection.query('SELECT * from test', (error, results, fields) => {
console.log(results);
return results;
});
}
The console displays the following data:
[ RowDataPacket {
id: 1,
firstName: 'test',
middleName: '1',
lastName: 'user' },
RowDataPacket {
id: 2,
firstName: 'test',
middleName: '2',
lastName: 'user' }
]
However, it's also generating the error:
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
EDIT:
To resolve this issue, I attempted a solution recommended by @Kim Kern:
return await connection.query('SELECT * from users', (error, results, fields) => {
results = results.map((result) => {
return Object.assign({}, result);
});
console.log(results);
return results;
});
Although the updated results no longer display RowDataPacket
, the same error persists.