Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

javascript - Angular 6: TypeError: Cannot read property 'value' of undefined

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think templateRef var should be on select element instead option element.

Instead this

<option selected *ngFor="let city of cities" [ngValue]="city.name" #name>{{city.name}}</option>

Can you try this:

<select class="custom-select" id="inputGroupSelect01">

On a side note templateRef variable name should unique for each select element. You can not use same variable for two different elements.

See #name here:

1. <option selected *ngFor="let city of cities" [ngValue]="city.name" #name>{{city.name}}</option>

2. <option selected *ngFor="let hotel of hotels" [ngValue]="hotel.name" #name>{{hotel.name}}</option>

Let me know, if that helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...