GitHub Documentation

Schema with references to another declared elsewhere



  • Hi,
    I'm trying to start my server, and it tells me it is having trouble compiling one of my schema files (at the line where it refers to another schema defined in another file). I can bring it into the same file but it still has the problem that it references this schema in the other schema as well.

    e.g:

    export class KawadiPlayer extends Schema {
    
      @type("int8")
      teamColor : number;
    
      @type("boolean")
      isPromoted : boolean;
    
      @type(KawadiTest)
      teamHome : KawadiTest;
    
    

    while KawadiTest is defined like so:

    import { Schema, type } from "@colyseus/schema";
    import { KawadiPlayer } from "./KawadiPlayer";
    
    export class KawadiTest extends Schema {
    
        @type(KawadiPlayer)
        owner : KawadiPlayer;
    }
    

    It gives me an error when I start this server, complaining about :

    TypeError: Cannot read property 'map' of undefined
        at Function.MapSchema.is (/Users/alampara/Documents/Colyseus/colyseus-kawadi/node_modules/@colyseus/schema/src/types/MapSchema.ts:64:20)
        at /Users/alampara/Documents/Colyseus/colyseus-kawadi/node_modules/@colyseus/schema/src/annotations.ts:205:45
        at __decorate (/Users/alampara/Documents/Colyseus/colyseus-kawadi/src/rooms/schema/KawadiTest.ts:5:110)
        at Object.<anonymous> (/Users/alampara/Documents/Colyseus/colyseus-kawadi/src/rooms/schema/KawadiTest.ts:7:5)
        at Module._compile (module.js:653:30)
        at Module._compile (/Users/alampara/Documents/Colyseus/colyseus-kawadi/node_modules/source-map-support/source-map-support.js:547:25)
        at Module.m._compile (/private/var/folders/tm/w9wrv6315kv7wvy94gwvh4780000gq/T/ts-node-dev-hook-44659108076979726.js:69:33)
        at Module._extensions..js (module.js:664:10)
        at require.extensions..jsx.require.extensions..js (/private/var/folders/tm/w9wrv6315kv7wvy94gwvh4780000gq/T/ts-node-dev-hook-44659108076979726.js:114:20)
        at require.extensions.(anonymous function) (/private/var/folders/tm/w9wrv6315kv7wvy94gwvh4780000gq/T/ts-node-dev-hook-44659108076979726.js:71:20)
    [ERROR] 20:52:47 TypeError: Cannot read property 'map' of undefined
    

    Would I be able to have cross references between two types?
    Thanks for any help you can provide.


  • administrator

    Hi @metiscoda,

    You can't have circular dependencies between your schema files. Your KamadiTest is requiring KawadiPlayer, which requires KamadiTest again.

    I suggest refactoring your structures so you don't have circular dependencies. Cheers!


 

© 2021 Lucid Sight, Inc