GitHub Documentation Patreon donate button

When running server: "TypeError: Cannot read property 'types' of undefined"



  • Hi, I'm running into a persistent error when trying to run a colyseus server. It starts up fine, but when I try to connect to the server from the client, I get the following error:

    > ts-node src/index.ts
    
    [gltd-server] running environment:  local
    ~you are now globally limited~
    [gltd-server] Listening on 2657
    TypeError: Cannot read property 'types' of undefined
        at Function.Reflection.encode (/Users/brianableson/gltd/globally.ltd/server/node_modules/@colyseus/schema/src/Reflection.ts:96:47)
        at SchemaSerializer.handshake (/Users/brianableson/gltd/globally.ltd/server/node_modules/colyseus/lib/serializer/SchemaSerializer.js:57:70)
        at Club.<anonymous> (/Users/brianableson/gltd/globally.ltd/server/node_modules/colyseus/lib/Room.js:292:136)
        at Generator.next (<anonymous>)
        at fulfilled (/Users/brianableson/gltd/globally.ltd/server/node_modules/colyseus/lib/Room.js:5:58)
        at processTicksAndRejections (internal/process/task_queues.js:97:5)
    

    I am closely following this example.

    Here is my current package.json file:

    {
      "name": "globally-ltd-game-server",
      "version": "0.0.1",
      "description": "",
      "main": "index.js",
      "scripts": {
        "start-local": "NODE_ENV=local onchange 'src/**.ts' -i -d 1000 -- ts-node src/index.ts",
        "start-ts": "ts-node src/index.ts",
        "start": "ts-node src/index.ts",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "@types/cors": "0.0.33",
        "@types/express": "^4.17.6",
        "@types/node": "^13.11.1",
        "onchange": "^3.3.0",
        "ts-node": "^7.0.1",
        "typescript": "^3.8.3"
      },
      "dependencies": {
        "colyseus": "^0.12.6",
        "cors": "^2.8.1",
        "express": "^4.14.0",
        "onchange": "^3.3.0"
      }
    }
    

    I'm new to typescript, so i'm guessing this is a noobish problem but i'm having trouble googling for the error. Thanks so much for your help in advance!



  • I am seeing this relevant post but am having trouble figuring out exactly what was changed:

    https://discuss.colyseus.io/topic/298/types-of-undefined-in-reflection-js-error-in-unity/1



  • @globallyltd I have this error when I use and empty State. Then I try create a property in that state object and it works.



  • @boygiandi Could you paste some code please, I'm having the same trouble but I can't fix it with your method :/


  • administrator

    Hi @Estournet

    Apparently you need to have some field inside your state:

    import { Schema, type } from "@colyseus/schema";
    
    class YourState extends Schema {
      @type("string") xxxx;
    }
    

    I'll check what can I do on the next version to prevent this error from happening. Let me know if that fixes for you.

    Cheers!



  • Hi @endel

    Okay I figured out how to do. I had this trouble and I am using plain JS, which adds more trouble :p (and I hadn't read the documentation about State, I have to admit)
    So, if anyone has the same trouble, here is a solution for plain Javascript :

    import { Room } from "colyseus";
    // Imports need to be done with require for Schema
    const schema = require('@colyseus/schema');
    const { defineTypes, Schema } = schema;
    
    export class TestState extends Schema {
      foo = "bar";
      anything = "you want"
    }
    
    export class TestRoom extends Room {
      onCreate(options) {
        this.setState(new TestState());
      }
    }
    
    // This is the important part
    defineTypes(TestState, {
      whatever: "string"
    });
    

    Thanks for the help !


 

GitHub Documentation Patreon donate button

© 2020 Endel Dreyer