Hi
I'm new with angular 4 and javascript
I've started a project and install the mysql npm.
Do not seem to find how i can use it in TypeScript.
Can someone point me in the right direction?
1. install mysql
2. In the angular.cli.json add
"scripts": [
"../node_modules/mysql/index.js"
],
2. create a service
import { Injectable } from '@angular/core';
import * as mysql from 'mysql';
@Injectable()
export class MysqlService {
constructor( ) { }
onGetData() {
console.log('In MysqlService onGetData');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'latest'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
connection.query('SELECT * from users limit 1 ', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
}
}
call the function from a button.
in the Consol log i see "In MysqlService onGetData", so I know i go this far with out issue.
they in the console i see the error
SearchFormComponent.html:7 ERROR TypeError: Net.createConnection is not a function
at Connection.connect (Connection.js:91)
at MysqlService.webpackJsonp.../../../../../src/app/services/mysql.service.ts.MysqlService.onGetData (mysql.service.ts:24)
at SearchFormComponent.webpackJsonp.../../../../../src/app/search-form/search-form.component.ts.SearchFormComponent.onSearch (search-form.component.ts:22)
at Object.eval [as handleEvent] (SearchFormComponent.html:7)
at handleEvent (core.es5.js:12047)
at callWithDebugContext (core.es5.js:13508)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:13096)
at dispatchEvent (core.es5.js:8659)
at core.es5.js:10821
at SafeSubscriber.schedulerFn [as _next] (core.es5.js:3647)
thank you for your help
Benoit L.