Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashed when use some of parameter decorators #16

Closed
Diluka opened this issue Aug 4, 2016 · 8 comments
Closed

Crashed when use some of parameter decorators #16

Diluka opened this issue Aug 4, 2016 · 8 comments

Comments

@Diluka
Copy link
Contributor

Diluka commented Aug 4, 2016

When I use @Body(), @Param(), @QueryParam() and etc, program will crash with this error

        var format = Reflect.getMetadata("design:paramtypes", object, methodName)[index];
                                                                                 ^

TypeError: Cannot read property '0' of undefined

routing-controllers 0.6.2
reflect-metadata 0.1.3
node 6.3.1

@pleerock
Copy link
Contributor

pleerock commented Aug 4, 2016

please provide a minimal reproduce code sample

@JD-Robbs
Copy link
Contributor

JD-Robbs commented Aug 7, 2016

I've run in to the same issue with the near-original PostController.ts from the samples - pasted below, including full error message.

  • routing-controllers 0.6.2

  • reflect-metadata 0.1.4

  • node 6.3.0

  • npm 3.10.5

  • typescript 1.8.9

  • target es6

  • experimentalDecorators true

    import {Request} from "express";
    import {JsonController, Get, Post, Put, Patch, Delete, Req} from "routing-controllers";
    
    @JsonController()
    export class PostController {
    
        @Get("/posts")
        getAll() {
            return [
                {id: 1, name: "First post!"},
                {id: 2, name: "Second post!"}
            ];
        }
    
        @Get("/posts/:id")
        getOne() {
            return {id: 1, name: "First post!"};
        }
    
        @Post("/posts")
        post(@Req() request: Request) {
            let post = JSON.stringify(request.body);
            return "Post " + post + " !saved!";
        }
    
        @Put("/posts/:id")
        put(@Req() request: Request) {
            return "Post #" + request.params.id + " has been putted!";
        }
    
        @Patch("/posts/:id")
        patch(@Req() request: Request) {
            return "Post #" + request.params.id + " has been patched!";
        }
    
        @Delete("/posts/:id")
        remove(@Req() request: Request) {
            return "Post #" + request.params.id + " has been removed!";
        }
    
    }
    

And the launcher (the server gets executed/listener started by ./bin/www):

"use strict";

import "reflect-metadata";
import "es6-shim";

import * as bodyParser from "body-parser";
import * as path from "path";
import * as express from "express";
import {useExpressServer} from "routing-controllers";

class Server {

    public app: express.Application;

    public static bootstrap(): Server {
        return new Server();
    }

    constructor() {
        this.app = express();
        this.config();
    }

    private config() {
        useExpressServer(this.app, {
            controllerDirs: [path.join(__dirname, "/controllers/*.js")] // register controllers routes in our express app
        });

        this.app.set("views", path.join(__dirname, "views"));
        this.app.set("view engine", "ejs");

        this.app.use(bodyParser.json());

        this.app.use(bodyParser.urlencoded({extended: true}));
        this.app.use(express.static(path.join(__dirname, "../public")));
        this.app.disable("x-powered-by");

    }
}

let server = Server.bootstrap();
export = server.app;

The full error:

node ./bin/www

F:\Downloads\myapp\node_modules\routing-controllers\decorator\params.js:10
var reflectedType = Reflect.getMetadata("design:paramtypes", object, methodName)[index];
^

TypeError: Cannot read property '0' of undefined
at F:\Downloads\myapp\node_modules\routing-controllers\decorator\params.js:10:89
at F:\Downloads\myapp\server\controllers\PostController.js:9:37
at DecoratePropertyWithDescriptor (F:\Downloads\myapp\node_modules\reflect-metadata\Reflect.js:549:29)
at Object.decorate (F:\Downloads\myapp\node_modules\reflect-metadata\Reflect.js:79:20)
at __decorate (F:\Downloads\myapp\server\controllers\PostController.js:4:92)
at Object.<anonymous> (F:\Downloads\myapp\server\controllers\PostController.js:42:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)

P.S.: I ❤️ this repo! 😃

P.P.S.:

Instead of

controllerDirs: [path.join(__dirname, "/controllers/*.js")]

I also tried

controllerDirs: [path.join(__dirname, "/controllers")]

While the server now starts without complaining, routing-controllers is now not attempting to load the controllers anymore.

@Diluka
Copy link
Contributor Author

Diluka commented Aug 8, 2016

@pleerock
Copy link
Contributor

pleerock commented Aug 9, 2016

@Diluka @JD-Robbs don't forget to add

{
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
}

in your tsconfig.json

@pleerock
Copy link
Contributor

pleerock commented Aug 9, 2016

@JD-Robbs

controllerDirs: [path.join(__dirname, "/controllers/*.js")]

this one is correct

@JD-Robbs
Copy link
Contributor

JD-Robbs commented Aug 9, 2016

You're awesome @pleerock - thanks a bunch for the pointer. Mea culpa!

emitDecoratorMetadata was the missing culprit in my case.

Like I said - this is a great project. As I'm transitioning to node from having worked with PHP MVCs for seven years, I'm really feeling at home now in the way I'm able to structure my code.

@Diluka
Copy link
Contributor Author

Diluka commented Aug 9, 2016

thanks for solving this problem

neither reflect-metadata nor tsc tells me about this option emitDecoratorMetadata, I see you already add a reminder, great!

i think this issue could be closed now

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants