无法从Firebase Angular2中检索数据

时间:2017-04-17 04:22:20

标签: html firebase angularfire2

firebase.service.ts中检索数据的方法

getListingDetails(id){
this.listing = this.af.database.object('records/listings/'+id) as FirebaseObjectObservable<Listing>
return this.listing;
}
listing.component.html中的

代码显示检索到的数据

    <div ng-if="listing">
    <a [routerLink]="['/listings']">Go Back</a>
    <br>
    <h2 class="page-header">{{listing.title}} <small>{{listing.city}}</small></h2>
    <div class="row">
    <div class="col-md-4">
     <!--IMAGE HERE -->
    </div>
    <div class="col-md-8">
    <ul class="list-group">
      <li class="list-group-item">Owner: {{listing.owner}}</li>
      <li class="list-group-item">Bedrooms: {{listing.bedrooms}}</li>
      <li class="list-group-item">Type: {{listing.type}}</li>
      <li class="list-group-item">Price: {{listing.price}}</li>
     </ul>
    </div>
    <a class="btn btn-default" href="#">Edit</a>
  <a class="btn btn-danger" href="#">Delete</a>
</div>
</div>

浏览器控制台中显示错误

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'title' of undefined
   TypeError: Cannot read property 'title' of undefined
   at Object.eval [as updateRenderer] (ng:///AppModule/ListingComponent.ngfactory.js:188:31)

at Object.debugUpdateRenderer [as updateRenderer]

但是在这些错误之后,对象被检索但没有显示在html标签中

Object {bedrooms: 15, city: "San Francisco", image: "mansion1.jpg", owner:   "Bruce Springstien", path: "/img/mansion1.jpg"…}

1 个答案:

答案 0 :(得分:0)

yours: <li class="list-group-item">Owner: {{listing.owner}}</li>

Answer: <li class="list-group-item">Owner: {{listing?.owner}}</li>

?通配符检查并解决您的问题