簡體   English   中英

Angular - 無法讀取未定義的屬性“id”

[英]Angular - Cannot read property 'id' of undefined

每當我嘗試創建新票證時,都會收到以下錯誤消息:

無法讀取未定義的屬性“id”

這是我的 thread.service.ts,我在其中使用了用於創建票證的 AddTicket 方法:

 addTicket(
id: string,
heading: string,
user: string,
date: Date,
description: string,
likeCount: number,
timeLeft: number){
  const newTicket = new Threads
  (
    id,
    heading,
    user,
    new Date(date),
    description,
    likeCount,
    timeLeft
  );
  let generatedId: string;
  return this.http
  .post<{ name: string }>(
    'https://ionic-angular-a0570.firebaseio.com/all-tickets.json',
    {
      ...newTicket,
      id: Math.random().toString()
    }
  )
  .pipe(
    switchMap(resData => {
      generatedId = resData.name;
      return this.tickets;
    }),
    take(1),
    tap(tickets => {
      newTicket.id = generatedId;
      this._tickets.next(tickets.concat(newTicket));
    })
  );
}

我正在調用 new-thread.page.ts 中的方法:

  onCreateTicket(){
this.threadService.addTicket(this.threads.id, this.heading, this.registerService.user, this.threadService.getDate(), this.description, +this.threadService.getLikes(), +12).subscribe();

如果有幫助,這是我的threads.model.ts:

export class Threads {
constructor(
    public id: string,
    public heading: string,
    public user: string,
    public date: Date,
    public description: string,
    public likeCount: number,
    public timeLeft: number
){}}

新線程.page.ts:

import { Component, OnInit } from '@angular/core';
import { ThreadsService } from '../main/departmentforum/threads/threads.service';
import { NavController } from '@ionic/angular';
import { RegisterService } from '../register/register.service';
import { Threads } from '../main/departmentforum/threads/threads.model';


@Component({
  selector: 'app-new-thread',
  templateUrl: './new-thread.page.html',
  styleUrls: ['./new-thread.page.scss'],
})
export class NewThreadPage implements OnInit {
  heading: string;
  description: string;
  businessforum: string;
  departmentforum: string;
  threads: Threads;

  constructor(private navController: NavController, private threadService: ThreadsService, public registerService: RegisterService) {}

  ngOnInit() {
  }

  onCreateTicket(){
    this.threadService.addTicket(this.threads.id, this.heading, this.registerService.user, this.threadService.getDate(), this.description, +this.threadService.getLikes(), +12).subscribe();
    this.navController.navigateBack('main/departmentforum/threads');
  }
}

thread.model.ts:

export class Threads {
constructor(
    public id: string,
    public heading: string,
    public user: string,
    public date: Date,
    public description: string,
    public likeCount: number,
    public timeLeft: number
){}}

new-thread.page.ts 中的threads變量被聲明但從未初始化。 它應該用一些數據初始化。 例如。

threads: Threads = new Threads(
  '0', 
  '', 
  '', 
  new Date((new Date()).getTime() + 24*60*60*1000), 
  '', 
  null, 
  null
);

工作示例: Stackblitz

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM