I have been working on an Ionic/Angular project and I'm facing difficulties in establishing a connection with my MySQL database (mariadb). Despite trying various solutions from online sources, I keep encountering numerous error messages. Any guidance on the most effective method to connect my MySQL database to my Ionic/Angular application would be highly appreciated.
Let's consider that my database is located on server 11.11.111.111, named 'products', with a table called 'items'. My login credentials are username: hain and password: 1234.
In order to address this issue, I've created a page for connection.service:
import { Injectable } from '@angular/core';
import { createPool, Pool } from 'mariadb';
@Injectable({
providedIn: 'root'
})
export class ConnectionService {
private pool: Pool = createPool({
host: '11.11.111.111',
user: 'hain',
password: '1234',
database: 'products',
connectionLimit: 5
});
constructor() { }
getItems(): Promise<any> {
return this.pool.getConnection()
.then(conn => {
return conn.query("SELECT * FROM items")
.then(rows => {
conn.release();
return rows;
})
.catch(err => {
conn.release();
throw err;
})
})
.catch(err => {
throw err;
});
}
}
Additionally, I have a tab2 page:
import { Component, OnInit } from '@angular/core';
import { ConnectionService } from '../services/connection.service';
@Component({
selector: 'app-tab2',
templateUrl: 'tab2.page.html',
styleUrls: ['tab2.page.scss']
})
export class Tab2Page implements OnInit {
product: any;
constructor(private connectionService: ConnectionService) {}
ngOnInit() {
this.ConnectionService.getItems().then((data) => {
this.product = data;
}).catch((err) => {
console.log(err);
});
}
}
The following type of errors are being displayed:
./node_modules/mariadb/lib/cmd/handshake/auth/caching-sha2-password-auth.js:2:11-24 - Error: Module not found: Error: Can't resolve 'fs' in …
[ng]
[ng] ./node_modules/mariadb/lib/cmd/handshake/auth/ed25519-password-auth.js:4:15-32 - Error: Module not found: Error: Can't resolve 'crypto' in …
I attempted resolving this by adding details to package.json:
"browser": {
"fs": false,
"net": false,
"tls": false,
"os": false,
"zlib": false,
"crypto": false
}
Despite researching extensively, configuring webpack.config.js did not yield successful results. If you have a more efficient or secure approach for handling a mariadb database, I am open to exploring all suggestions.