如何修复类型错误不是角度的功能

时间:2019-05-20 18:39:17

标签: angular

有一个简单的网站选择您自己的冒险组件。每当我尝试调用为代表故事事件而制作的类的函数时,都会出现此错误。

export class ZombieCharacterOutcome {
    id: string;
    text: string;
    public characteristic: ZombiePlayerCharacteristic;
    events: string[];

    public getNextEvent(): string {
        var rand = Math.random() * this.events.length;
        return this.events[rand];
    }
}

我正在使用Firestore数据库存储数据,这也是初始化新结果的方式。

getCharaterOutcome(outcomeID: string): Observable<ZombieCharacterOutcome> 
{
    console.log(outcomeID);
    return this.db.doc('characterOutcomes/' + 
    outcomeID).snapshotChanges().pipe(map(choice => {
    const data = choice.payload.data() as ZombieCharacterOutcome;
    if (data) {
       data.id = choice.payload.id;
    }
    return data;
  }));
}

这是来自Angular Component的代码在前端被调用...

nextEvent() {
    if(this.eventCount < this.maxEvent) {
        this.characterService.getCharacterEvent(this.currentOutcome.getNextEvent()).subscribe(event => {
            this.currentOutcome = null;
            this.currentEvent = event;
            this.loadChoices();
        });
    } else {
        this.currentEvent = null;
        this.currentChoices = [];
        this.currentOutcome = null;
    }
}
  

错误TypeError:this.currentOutcome.getNextEvent不是一个函数       在ZombieCharacterComponentComponent.push ../ src / app / zombie-character-component / zombie-character-component.component.ts.ZombieCharacterComponentComponent.nextEvent(zombie-character-component.component.ts:60)处       在Object.eval [作为handleEvent](ZombieCharacterComponentComponent.html:28)       在handleEvent(core.js:10251)       在callWithDebugContext(core.js:11344)       在Object.debugHandleEvent [作为handleEvent](core.js:11047)       在dispatchEvent(core.js:7710)       在core.js:8154       在HTMLAnchorElement。 (platform-b​​rowser.js:988)       在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invokeTask(zone.js:421)       在Object.onInvokeTask(core.js:3811)

1 个答案:

答案 0 :(得分:0)

ZombieCharacterOutcome变量尚未实例化。

我重新制作了getCharacterOutcomeCode,以在返回之前创建一个新的ZombieCharacterOutcome,并且现在可以正常工作。