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:
- Created a global variable for holding connection received from
mysql.createConnection()
. - 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.