类型{...}的参数不能分配给类型[]的参数

时间:2018-09-07 18:50:18

标签: database angular typescript firebase-realtime-database ionic3

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { EventDetail } from '../../models/event-detail/event-detail.interface';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';

@IonicPage()
@Component({
  selector: 'page-event',
  templateUrl: 'event.html',
})
export class EventPage {

  eventDetail = {} as EventDetail;

  eventDetailRef$: AngularFireList<EventDetail[]>;

  constructor(public navCtrl: NavController, public navParams: NavParams, private database: AngularFireDatabase) {
  this.eventDetailRef$ = this.database.list('event-detail');
  }

  addEvent( eventDetail: EventDetail) {

    this.eventDetailRef$.push({
      eventName: this.eventDetail.eventName,
      eventDesc: this.eventDetail.eventDesc,
    });

  }

}

我在一年前遵循了一个教程,我推荐了“升级到Angularfire 5.0”,用谷歌搜索了同样的问题,结果却大不相同

错误消息是:

“类型'{eventName:string; eventDesc:String;}'的参数不能分配给'EventDetail []'类型的参数。   对象文字只能指定已知属性,并且'EventName'在'EventDetail []'类型中不存在。”

和我的EventDetail界面:

export interface EventDetail {
    eventName: string;
    eventDesc: String;
    lat: number;
    lgt: number;

}

the error image

2 个答案:

答案 0 :(得分:0)

错误消息表明您正在尝试将{ eventName: string; eventDesc: String; }分配给EventDetail

数组

push方法需要一个数组,而不是单个实例。

答案 1 :(得分:0)

eventDetailRef$: AngularFireList<EventDetail[]>;更改为eventDetailRef$: AngularFireList<EventDetail>

因为您的类型是Array<Array>,并且您试图推送单个项目