Skn0tt / nextjs-nestjs-integration-example

https://nextjs-nestjs-integration-example.now.sh
156 stars 36 forks source link

It does not work correctly. #13

Closed yardz closed 3 years ago

yardz commented 3 years ago

Your example does not work correctly. When you create an endpoint that doesn’t receive parameters, okay, it works, but when you put a decorator inside a function it stops working.

@Get('/:_id')
async findOne(@Param() p) {
    return { p };
}

I'm trying to find a way to make everything work correctly, but so far without success

Skn0tt commented 3 years ago

Hi @yardz! Have you added the @babel/plugin-proposal-decorators plugin?

https://github.com/Skn0tt/nextjs-nestjs-integration-example/blob/6e21c091786a2e025e7289c7dd99a396284d88d7/.babelrc#L4

If that doesn't solve it, a reproduction case would be great.

yardz commented 3 years ago

In fact, actually reproducing the error is quite easy. Just add this code to your controller (src/backend/app/app.controller.ts):

    @Get("/:number")
    async findOne(@Param() param) {
        return { param }; // You don't even need a return, I put it just to have some return.
    }

If you prefer to download the file: app.controller.ts.zip

Captura de Tela 2021-01-22 às 4 57 40 PM

Then access the endpoint: http://localhost:3000/api/randomNumber/123

The terminal will display this error:

ready - started server on http://localhost:3000
event - compiled successfully
event - build page: /next/dist/pages/_error
wait  - compiling...
event - compiled successfully
event - build page: /api/[...catchAll]
wait  - compiling...
error - ./src/backend/app/app.controller.ts 11:16
Module parse failed: Unexpected character '@' (11:16)
File was processed with these loaders:
 * ./node_modules/next/dist/build/webpack/loaders/next-babel-loader.js
You may need an additional loader to handle the result of these loaders.
|   }
| 
>   async findOne(@Param()
|   param) {
|     return {

Ps: If you create a service, there is also an error, but I imagine the error is because of the decorators. Probably solving this error will solve the other.

Skn0tt commented 3 years ago

Got it! You need to add babel-plugin-parameter-decorator:

{
  "presets": ["next/babel"],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
+   "babel-plugin-parameter-decorator"
  ]
}