无法读取属性'推送'未定义的

时间:2017-10-07 14:25:55

标签: angular nativescript angular2-forms angular2-nativescript

我在打字稿上使用推送方法时遇到问题,详情请点击此处 这是我的预订模式组件代码:

guestArray=[1, 2, 3, 4, 5, 6];
guests: number;
isDateTime: boolean = false;

constructor(private params: ModalDialogParams,
    private page: Page) {

        if(params.context === "guest") {
            this.isDateTime = false;
        }
        else if(params.context === "date-time") {
            this.isDateTime = true;
        }
}

ngOnInit() {

    if (this.isDateTime) {
        let datePicker: DatePicker = <DatePicker>this.page.getViewById<DatePicker>('datePicker');

        let currentdate: Date = new Date();
        datePicker.year = currentdate.getFullYear();
        datePicker.month = currentdate.getMonth() + 1;
        datePicker.day = currentdate.getDate();
        datePicker.minDate = currentdate;
        datePicker.maxDate = new Date(2045, 4, 12);

        let timePicker: TimePicker = <TimePicker>this.page.getViewById<TimePicker>('timePicker');
        timePicker.hour = currentdate.getHours();
        timePicker.minute = currentdate.getMinutes();
    }
}

public submit() {
    if (this.isDateTime) {
        let datePicker: DatePicker = <DatePicker>this.page.getViewById<DatePicker>('datePicker');
        let selectedDate = datePicker.date;
        let timePicker: TimePicker = <TimePicker>this.page.getViewById<TimePicker>('timePicker');
        let selectedTime = timePicker.time;
        let reserveTime = new Date(selectedDate.getFullYear(),
                                    selectedDate.getMonth(),
                                    selectedDate.getDate(),
                                    selectedTime.getHours(),
                                    selectedTime.getMinutes());
        this.params.closeCallback(reserveTime.toISOString());
    }
    else {
        let picker = <ListPicker> this.page.getViewById<ListPicker>('guestPicker');
        this.params.closeCallback(this.guestArray[picker.selectedIndex])
    }
}

`

提交结果返回预约组件后,这里处理的是代码。

reservation: FormGroup;
formReservation: View;
formResult: View;
docId: string = "reservations";
doc: any;
db:any;

constructor(private changeDetectorRef: ChangeDetectorRef,
    private formBuilder: FormBuilder,
    private modalService: ModalDialogService,
    private vcRef: ViewContainerRef,
    private page: Page,
    private couchbaseService: CouchbaseService) {
    super(changeDetectorRef);

    this.reservation = this.formBuilder.group({
        guests: 3,
        smoking: false,
        dateTime: ['', Validators.required]
    });
    this.doc = this.couchbaseService.getDocument(this.docId);
}
createModalView(args) {

    let options: ModalDialogOptions = {
        viewContainerRef: this.vcRef,
        context: args,
        fullscreen: false
    };

    this.modalService.showModal(ReservationModalComponent, options)
        .then((result: any) => {
            if (args === "guest") {
                this.reservation.patchValue({ guests: result });
            }
            else if (args === "date-time") {
                this.reservation.patchValue({ dateTime: result });
            }
        });

}

ngOnInit() {
    this.formReservation = <View>this.page.getViewById<View>("form_reservation");
    this.formResult = <View>this.page.getViewById<View>("form_result");

    this.formResult.animate({
        opacity: 0,
        scale: { x: 0.5, y: 0.5 },
        duration: 0
    })
}

onSmokingChecked(args) {
    let smokingSwitch = <Switch>args.object;
    if (smokingSwitch.checked) {
        this.reservation.patchValue({ smoking: true });
    }
    else {
        this.reservation.patchValue({ smoking: false });
    }
}

onGuestChange(args) {
    let textField = <TextField>args.object;

    this.reservation.patchValue({ guests: textField.text });
}

onDateTimeChange(args) {
    let textField = <TextField>args.object;

    this.reservation.patchValue({ dateTime: textField.text });
}

onSubmit() {
    console.log(JSON.stringify(this.reservation.value));
    let reservationArr : Array<object>;

   reservationArr.push(this.reservation.value);
   console.log(JSON.stringify(reservationArr));

    this.formReservation.animate({
        opacity: 0,
        scale: { x: 0.5, y: 0.5 },
        duration: 500
    }).then(() => this.formResult.animate({
        opacity: 1,
        scale: { x: 1, y: 1 },
        duration: 500
    }));

    if (this.doc == null) {
        console.log("This is the first reservation");
        this.couchbaseService.createDocument({ "guests": this.reservation.value.guests, "smoking": this.reservation.value.smoking, "dateTime": this.reservation.value.dateTime }, this.docId);
    } else {
        console.log(this.doc.value);
        this.couchbaseService.updateDocument(this.docId, { "guests": this.reservation.value.guests, "smoking": this.reservation.value.smoking, "dateTime": this.reservation.value.dateTime });
    }
}

这是提交后我的日志图像我收到了数据,但我无法将其推入新阵列。你能检查并解释一下为什么没用吗?非常感谢你。

Log image

1 个答案:

答案 0 :(得分:4)

你只是声明数组而不是初始化它,它应该是,

   let reservationArr : Array<object> = [];