0

i want to show the data to the form and

i have this error:

TypeError: Cannot read property 'title' of undefined

heres my component :

  book:Book;
  getBook(){
    var id = this.route.snapshot.params['id'];
    this.bookService.getBook(id)
    .subscribe(book=>{
     this.book = book;
    })
  }

and this is my view

<div class="card col-md-5">
<div class="card-body">
    <form>
        <div class="form-group row">
          <label for="Title" class="col-sm-2 col-form-label">Title</label>
          <div class="col-sm-10">
            <p  class="form-control" disabled name="title" >{{book.title}}</p>
          </div>
        </div>
        <div class="form-group row">
          <label for="Author" class="col-sm-2 col-form-label">Author</label>
          <div class="col-sm-10">
            <p  class="form-control"  name="title">{{book.author}}</p>
          </div>
        </div>
        <div class="form-group row">
            <label for="Author" class="col-sm-2 col-form-label">ISBN</label>
            <div class="col-sm-10">
              <p  class="form-control" disabled name="title" >{{book.isbn}}</p>
            </div>
          </div>
        <div class="form-group row">
            <div class="col-sm-6">
              <!-- <button type="submit" class="btn btn-primary">SAVE</button> -->
              <button type="submit" class="btn btn-success" [routerLink] ="['/book']" >CANCEL</button>                
            </div>

        </div>
      </form>
</div>

is there anything wrong on my view or just on my component

Christian Benseler
  • 7,907
  • 8
  • 40
  • 71
Arza
  • 63
  • 2
  • 3
  • 10

1 Answers1

-1

You have to set a default value for this 'book' property

book:Book = new Book();

or use ngIf to ensure the 'book' exists

<div class="card-body" *ngIf="book">
Christian Benseler
  • 7,907
  • 8
  • 40
  • 71