1// How to check for unique validity in MEAN stack apps
2// Backend : Node + Express + MongoDB
3// DB-backend communication : mongoose + mongoose_unique_validator
4// Angular script to show validations errors in UI
5// You have to match angular's formControlNames with mongoose's model fields
6
7// Init
8fg: FormGroup
9constructor(private $http: HttpClient) {}
10
11// your http request
12this.$http.post('//my_post_url', data).subscribe(
13 (e) => {
14 console.log('All is well')
15 },
16 (error) => {
17 console.error('we got errors')
18 Object.keys(error.errors)
19 .map((errorKey) => errors[errorKey].path)
20 .forEach((path) => {
21 // path: name of mongoose schema field where unique error occured
22 this.fg.get(path).setErrors({ notUnique: true })
23 })
24 }
25)