Iām new to TypeScript so simple things still trip me up. I successfully created a database connection using the code below which was not in a class:
const mysql = require('mysql2');
let connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'myPW',
database: 'myDb'
});
connection.connect(function(err: any) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
All was fine. Then I put this same code inside of a class like this:
const mysql = require('mysql2');
export class Server {
constructor() { }
connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'myPW',
database: 'myDb'
});
connection.connect(function(err: any) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
}
and the I got these errors:
The errors below come from the red squiggly on line 13
Duplicate identifier 'connection'.
Unexpected token. A constructor, method, accessor, or property was expected.
'connect', which lacks return-type annotation, implicitly has an 'any' return type.
'function' is not allowed as a parameter name.
Duplicate function implementation.
Can someone kindly explain what I'm doing wrong here?
Thank you.