Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度-无法读取属性';id';未定义的_Javascript_Angular_Ionic Framework_Firebase Realtime Database - Fatal编程技术网

Javascript 角度-无法读取属性';id';未定义的

Javascript 角度-无法读取属性';id';未定义的,javascript,angular,ionic-framework,firebase-realtime-database,Javascript,Angular,Ionic Framework,Firebase Realtime Database,每当我尝试创建新票证时,都会收到以下错误消息: 无法读取未定义的属性“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”

这是我的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));
    })
  );
}
这是my 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
){}}
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();
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
);

工作示例:

您能说出引发错误的行吗?@MichaelD NewThreadPage.html:48错误类型错误:无法读取Object.eval[as handleEvent](NewThreadPage.html:48)中NewThreadPage.onCreateTicket(new thread.page.ts:26)的undefined属性“id”(新线程方法尚未初始化。你确定它是在调用之前定义的吗?@MichaelD我以为this.threads来自模型,只是一个模板。。所以我想我还没有初始化它。我如何或在哪里可以这样做?如果不查看new-thread.page.ts或它的模板,我就不能完全确定这一点。