Uncaught TypeError: Cannot read property '_schema' of undefined

Hi,

I'm having trouble using Schema for my state. If I comment the setState line it works.

Here's what I got:

import { Room, Client } from 'colyseus';
import { type, Schema } from '@colyseus/schema';

import { BattleState } from './BattleState';
import { MessageType } from '../utils';

export class BattleRoom extends Room {
    @type('number') maxClients = 2;

    onInit(options: any) {
        this.setState(new BattleState());
    }

    onJoin(client: Client, options: any) {
        console.log('onJoin', options);
        
        if (this.clients.length === 2) {
            this.lock();
        }
    }

    onLeave(client: Client, consented: boolean) {
        console.log('onLeave', consented);
    }

    onMessage(client: Client, message: any) {
        console.log('onMessage', message);
    }

    onDispose() {
        console.log('onDispose');
    }
}
import { MapSchema, Schema, type } from '@colyseus/schema';

import { BattleTeam } from '../entities/BattleTeam';

export class BattleState extends Schema {
    @type('uint8') round = 0;
    @type('string') status = 'READY';
    @type('uint8') moves = 0;
    @type('uint8') teamCount = 0;
    @type({ map: BattleTeam }) teams = new MapSchema<BattleTeam>();
}
import { Schema, type } from '@colyseus/schema';

import { BattleTeamData } from '../utils';

export class BattleTeam extends Schema {
    @type('uint8') move: number;

    constructor(teamData: BattleTeamData) {
        super();

        this.move = 0;

        console.log('---- BattleTeam ----');
        console.log(this.toJSON());
    }
}
interface BattleTeamData {
    move: number;
}

hi @hlarcher, welcome!

will I be able to reproduce just with the code you provided? I don't see you mutating much on the State for this error to happen. I'd appreciate if you can provide a simple client + server on GitHub so I can inspect this problem further.

This error (TypeError: Cannot read property '_schema' of undefined) indeed does happen sometimes, and it's quite difficult to track down. It usually happens when the encoder is not doing its job properly (or doing it in a not so intuitive way)

You're right, its not easy to test from what I've shown, I just assumed I was doing something obviously wrong that you would pickup off the bat.

I'll setup a repo with it, at the moment its split into 2 different ones and has a lot of unnecessary code.

Thanks again, I'll let you know when its done.

@endel while putting together the repo I managed to get it working because I linked to the client script in https://raw.githack.com/colyseus/colyseus.js/master/dist/colyseus.js instead of the local one I had, maybe it was just a bug in the old version I was running.