Angular Fire对象(类型“ {}”不可分配给类型)

时间:2018-07-01 16:09:24

标签: javascript angular firebase firebase-realtime-database angular6

我正在尝试从Firebase上的实时数据库中获取对象。将键(id)传递到我的餐厅服务后,似乎它没有返回我想要的特定对象。

新餐厅。ts

export class NewRestaurantComponent implements OnInit {
  id;
  restaurant$;
  restaurant: Restaurant = {
    $key: '',
    name: '',
    displayName: '',
    location: '',
    deliveryPrice: 0,
    imageUrl: '',
  };
  constructor(private restaurantService: RestaurantService,
    private af: AngularFireDatabase,
    private router: Router,
    private route: ActivatedRoute) {}

  ngOnInit() {
    this.route.params.forEach((urlParameters) => {
      this.id = urlParameters['id'];
    });

    if (this.id) {
      this.restaurantService.get(this.id).valueChanges()
        .subscribe(action => {
          this.restaurant = Object.assign({}, action)
        })
    }
  }
}

restaurantservice.ts

export class RestaurantService {
  constructor(private db: AngularFireDatabase) { }
  create(restaurant) {
    return this.db.list('/restaurants').push(restaurant);
  }

  getAll() {
    return this.db.list('/restaurants');
  }
  get(restaurantId) {
    return this.db.object('/restaurants/' + restaurantId);
  }
  update(restaurantId, restaurant) {
    return this.db.object('/restaurants/' + restaurantId).update(restaurant);
  }
  delete(restaurantId) {
    return this.db.object('/restaurants/' + restaurantId).remove();

  }
}

编译后还不错,但仍然掉错告诉我

Type '{}' is not assignable to type 'Restaurant'.Property 'name' is missing in type '{}'.

0 个答案:

没有答案