Currently, I am working on developing a fullstack application using Node.js and Angular (material UI). However, I have encountered an issue that I need help with.
I am trying to figure out how to retrieve data from an Angular form for a small web resource management application. Right now, I am in the process of registering users and storing their information in a MySQL database.
https://i.sstatic.net/liHPd.png
On the server side, we have the following code:
const express = require('express');
const app = express();
var cors = require('cors');
var mysql = require('mariadb');
const body = require("body-parser");
app.use(body.urlencoded({ extended: false }));
app.use(body.json());
app.use(cors());
var connection = mysql.createPool({
host : 'localhost',
user : 'root',
password : '',
database : 'totale',
});
app.get('/', function (req, res) {
res.send('Hello World!');
console.log(req);
});
app.post("/inscription",(req,res)=>{
var nom = req.body.name;
var prenom = req.body.lastname;
var mail = req.body.email;
var password = req.body.password;
connection.getConnection().then(conn => {
conn.query("INSERT INTO `inscription` (nom,prenom,mail,password) values (?,?,?,?);",[nom,prenom,mail,password],function(err, rows) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
});
});
app.listen(3000, function () {
console.log('app listening on port 3000!');
});
Here is the HTML form structure:
<form [formGroup]="signupForm" class="example-form" (ngSubmit)="onFormSubmit(signupForm)" novalidate>
<mat-form-field class="example-full-width">
<input type="text" id='email' name='email' matInput placeholder="Enter your email"/>
</mat-form-field>
<mat-form-field class="example-full-width">
<input type="text" id='name' name='name' matInput placeholder="Nom">
</mat-form-field>
<mat-form-field class="example-full-width">
<input type="text" id='lastname' name='lastname' matInput placeholder="Prenom" >
</mat-form-field>
<mat-form-field class="example-full-width">
<input type="password" id='password' name='password' type="password" matInput placeholder="Enter your password" [type]="hide ? 'password' : 'text'">
<button mat-icon-button matSuffix (click)="hide = !hide" [attr.aria-label]="'Hide password'" [attr.aria-pressed]="hide">
</button>
</mat-form-field>
<div class="example-button-row">
<button type="submit" mat-raised-button>Inscription</button>
</div>
</form>
In TypeScript, we handle the data retrieval like this:
import { Component, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
import { User } from './../User';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import {tap} from 'rxjs/operators';
import {NgForm} from '@angular/forms';
@Component({
selector: 'app-inscription',
templateUrl: './inscription.component.html',
styleUrls: ['./inscription.component.css']
})
export class InscriptionComponent implements OnInit {
signupForm;
results;
constructor (private http: HttpClient, private formBuilder: FormBuilder)
{}
ngOnInit() {
this.signupForm = this.formBuilder.group({
email :"",
name :"",
lastname :"",
password :"",
});
}
public onFormSubmit() {
const configUrl = 'http://localhost:3000/inscription';
this.http.post(configUrl,this.signupForm.value)
.pipe(
tap(
data => console.log(configUrl, data),
error => console.log(configUrl, error)
)
)
.subscribe(results => this.results = results);
}
}