I am creating a simple form to submit data , email, city name and hotel name, when I click submit i get this error:
AboutComponent.html:79 ERROR TypeError: Cannot read property 'value'
of undefined
Here is what i have :
comment.js
// Comments Schema
const CommentsSchema = mongoose.Schema({
email: {
type: String,
required: true
},
name: {
type: String,
required: true
}
},{
collection: 'comments'
});
const Comments = module.exports = mongoose.model('Comments', CommentsSchema);
component.ts
addReview(email, name) {
this.moviesService.addReview(email, name).subscribe(success => {
this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
// get the id
this.activeRouter.params.subscribe((params) => {
// tslint:disable-next-line:prefer-const
let id = params['id'];
this.moviesService.getComments(id)
.subscribe(comments => {
console.log(comments);
this.comments = comments;
});
});
}, error => {
this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
});
}
html
<form [formGroup]="angForm" class="form-element">
<div class="form-group form-element_email">
<input type="email" class="form-control info" placeholder="Email" formControlName="email" #email (ngModelChange)="onChange($event)">
</div>
<div *ngIf="angForm.controls['email'].invalid && (angForm.controls['email'].dirty || angForm.controls['email'].touched)"
class="alert alert-danger">
<div *ngIf="angForm.controls['email'].errors.required">
Email is required.
</div>
</div>
<div class="input-group mb-3 form-element_city">
<select class="custom-select" id="inputGroupSelect01">
<option selected *ngFor="let city of cities" [ngValue]="city.name" #name>{{city.name}}</option>
</select>
</div>
<div class="input-group mb-3 form-element_hotel">
<select class="custom-select" id="inputGroupSelect01">
<option selected *ngFor="let hotel of hotels" [ngValue]="hotel.name" #name>{{hotel.name}}</option>
</select>
</div>
<div class="form-group">
<button type="submit" (click)="addReview(email.value, name.value)" class="btn btn-primary btn-block form-element_btn" [disabled]="!validEmail">ACCEDER</button>
</div>
</form>
service.ts
addReview(email, name): Observable<any> {
const uri = `${apiUrl + this.addCommentsUrl}`;
const obj = {
email: email,
name: name
};
return this.http.post(uri, obj);
}
Note: when I remove name everything works fine , but i need also city name and hotel name to be inserted to my database together with email.
what is wrong with my code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…