Новое в Angular и Nodejs. когда я отправляю форму регистрации, она выполняется успешно. в mysql я выбираю адрес электронной почты и номер мобильного телефона как уникальные. через некоторое время он выдал ошибку при отправке дубликата по электронной почте. Если я использую условия findOne и findmobile в Auth. js, форма файла не отправлена. но ошибка показывает twicw. Вот мой код:
Сервисный файл в Angular
url = `${environment.apiUrl}/Auth`;
httpOptions = {
headers: new HttpHeaders({
'contentType': 'application/json'
})
}
constructor(private http: HttpClient) { }
signUp(userForm: User) {
return this.http.post(`${this.url}/signUp`, userForm,
this.httpOptions).pipe(catchError(this.handleError))}
}
**SignUp Component.ts**
onSignup() {
this.userForm = this.registerForm.value;
console.log(this.userForm)
this._signupService.signUp(this.userForm).subscribe(res => {
console.log(res)
}),
err => {console.log(err)}
this.registerForm.reset();
this.pwdstatus="";
}
###**db.js file**###
```
const mysql = require("mysql")
const connection = mysql.createConnection({
host:"localhost",
port:3306,
user: 'root',
password: "123456",
database: "EMS"
})
connection.connect(function(err){
if(err) throw err;
console.log("mysql connected")
})
module.exports = connection;
```
###**Model.js file**###
```
var db = require("../db")
let model = {
signup: (input, cb) => {
let today = new Date();
let data = {
organisationName: input.organisationName,
email: input.email,
mobileNumber: input.mobileNumber,
password: input.password,
is_Active: 1,
createdAt: today
}
return db.query('INSERT INTO users SET ?' ,[data],(err,result)=>{
console.log("Error");
console.log(err);
console.log("result");
console.log(result);
})
},
findOne: (email,cb) => {
return db.query("SELECT * FROM users WHERE email=? AND is_Active=1",[email],cb);
},
findMobile: (mobileNumber,cb) => {
return db.query("SELECT * FROM users WHERE mobileNumber=? AND is_Active=1",
[mobileNumber],cb);
}
}
module.exports = model;
###**Auth.js file**###
```
var express = require("express")
var router = express.Router()
var user = require("../Models/model")
var bcrypt = require("bcrypt")
//router.get('/signUP',(req,res)=> res.send("SignUp Page"));
router.post('/signUp', (req, response) => {
var email = req.body.email;
var mobile = req.body.mobileNumber;
if (user.findOne(email, (err, res) => {
if (err) console.log(err);
if (res.length > 0) {
response.send(`This Email: ${email} already registered !!!`);
return true;
}
})) { }
if (user.findMobile(mobile, (err, res) => {
if (err) console.log(err);
if (res.length > 0) {
response.send(`This Mobile no: ${mobile} already registered !!!`);
return true;
}
})) { }
else
user.signup(req.body, (error, result) => {
response.json({ data: result, error: error })
})
})
module.exports = router;
```
#**WHEN Form submit**#
**inCOnsole**
```
OPTIONS /Auth/signUp 204 4.866 ms - 0
Error
null
result
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 5,
serverStatus: 2,
warningCount: 1,
message: '',
protocol41: true,
changedRows: 0
}
```
**After few minutes**
>
POST /Auth/signUp - - ms - -
Error
Error: ER_DUP_ENTRY: Duplicate entry 'paggy@gmail' for key 'users.email_UNIQUE'
at Query.Sequence._packetToError (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Angular\EMSexpress\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (D:\Angular\EMSexpress\node_modules\mysql\lib\Connection.js:526:10)
BLAH... BLAH...BLAH