标签: nestjs

未找到类验证器的元数据

我正在尝试使用 aValidationPipe但无论我如何编写代码,在发送请求时都会收到以下警告:No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.

我的路线看起来像这样:

@Get()
@UsePipes(new ValidationPipe({ transform: true }))
async findAll(@Query() queryDto: QueryDto) {
    return await this.myService.findAll(queryDto);
}
Run Code Online (Sandbox Code Playgroud)

我的 DTO 看起来像这样:

export class queryDto
{
    @ApiModelProperty({
        description: 'Maximum number of results',
        type: Number,
        example: 50,
        default: 50,
        required: false
    })
    readonly limit: number = 50;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用ValidationPipe多种方法,遵循doc,但没有任何效果对我有用。我知道这是行不通的,因为虽然请求得到响应,默认值,我在我的DTO写的财产limit,这是50,是不是当查询是空的使用。因此,当limit查询中提供no 时,limit …

javascript node.js typescript class-validator nestjs

10
推荐指数
1
解决办法
7638
查看次数

In nest.js, is it possible to get service instance inside a param decorator?

I want to achieve something like this using nest.js: (something very similar with Spring framework)

@Controller('/test')
class TestController {
  @Get()
  get(@Principal() principal: Principal) {

  }
}
Run Code Online (Sandbox Code Playgroud)

After hours of reading documentation, I found that nest.js supports creating custom decorator. So I decided to implement my own @Principal decorator. The decorator is responsible for retrieving access token from http header and get principal of user from my own auth service using the token.

import { createParamDecorator } from '@nestjs/common';

export const …
Run Code Online (Sandbox Code Playgroud)

javascript decorator node.js typescript nestjs

10
推荐指数
2
解决办法
5922
查看次数

带有 NestJS + Typescript + Webpack + node_modules 的单个文件包

如何?我如何捆绑 NestJS 项目,包括用于离线应用的 node_module?

webpack.config.js

const path = require('path');

module.exports = {
  entry: path.join(__dirname, 'dist/main.js'),
  target: 'node',
  output: {
    filename: 'compiled.js',
    path: __dirname,
  },
  resolve: {
    alias: {
      node_modules: path.join(__dirname, 'node_modules'),
    },
    extensions: ['.js'],
  },
};
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "kai-brs",
  "version": "0.9.1",
  "author": "Sovgut Sergey",
  "private": true,
  "scripts": {
    "build:webpack": "rimraf dist && tsc -p tsconfig.build.json && webpack dist/main.js -o dist/main.bundle.js --mode=production",
    "build": "tsc -p tsconfig.build.json",
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "nodemon",
    "start:debug": "nodemon --config …
Run Code Online (Sandbox Code Playgroud)

javascript node.js typescript webpack nestjs

10
推荐指数
1
解决办法
4311
查看次数

枚举不适用于 nestjs 和 graphql

我最近从直接使用 typegraphql 和 typeorm 转向将它们与 nestjs 一起使用。大多数情况下,这是一次直接的体验。然而,我有一个关于枚举的问题。

我有一组组合在一起的自定义装饰器,这样我就可以轻松地装饰我的模型,而无需同时使用 typeorm、typegraphql 和类验证器装饰器。这以前很好用,现在在除枚举以外的所有情况下都很好用。

作为一个例子,这里是一个@OptionalDecimal 装饰器:

import { IsNumber } from 'class-validator'
import { Field, Float } from 'type-graphql'
import { Column } from 'typeorm'

export function OptionalDecimal() {
    const typeDecorator = IsNumber()
    const fieldDecorator = Field(type => Float, { nullable: true })
    const columnDecorator = Column('decimal', { nullable: true })

    return (target: any, key: string) => {
        typeDecorator(target, key)
        fieldDecorator(target, key)
        columnDecorator(target, key)
    }
}
Run Code Online (Sandbox Code Playgroud)

我的@Enum 装饰器是这样的:

import { IsEnum } from 'class-validator' …
Run Code Online (Sandbox Code Playgroud)

enums graphql typeorm nestjs typegraphql

10
推荐指数
1
解决办法
4154
查看次数

我尝试过 nestjs,但我意识到它因为有太多的装饰器而降低了代码的可读性,请花点时间访问这个

我最近使用了 nestjs,但我意识到它过于复杂,我的意思是看下面的代码:

@post('/products')
getAllProducts(@Body('title') title, @Body('price') price, @Body('description') description) { }
Run Code Online (Sandbox Code Playgroud)

它使函数参数变得很脏,而且函数上方可能还有更多装饰器,如@Header、@Params 等。在我看来,这会降低可读性。nodejs中的相同代码

const { title: title, price: price, description: description } = req.body
Run Code Online (Sandbox Code Playgroud)

nodejs 更具可读性...

然后我研究了为什么开发人员使用 nestjs,原因是模块化。为什么我们不自己实现这个......

见下文:

查看我的目录结构

在 app.js 中,我刚刚踢了应用程序:

const express = require('express');

const app = express();

// express config
require('./startup/config')(app);

// handling routes
require('./startup/routes')(app);

// db setup
require('./startup/db')(app);
Run Code Online (Sandbox Code Playgroud)

在启动文件夹中,我做了一些基本的工作,比如 mongoose 配置和连接到 db 等。

但是,在启动/路由中,我只是按如下方式踢了模块:

const shopModule = require('../shop/shop.module');

module.exports = app => {
    app.use('/', shopModule);
};
Run Code Online (Sandbox Code Playgroud)

在商店模块中,我只是踢了如下路线:

const router = require('express').Router();

const productsRouter = require('./products/index'); …
Run Code Online (Sandbox Code Playgroud)

node.js nestjs

10
推荐指数
1
解决办法
3184
查看次数

Nest.Js 不接受任何更改

我尝试在AppController 中创建一个新方法,但它没有反映更改。我什至尝试更改默认的getHello()方法,但它输出“Hello World!” . 这怎么可能?

失眠

在此处输入图片说明

应用控制器

在此处输入图片说明

应用服务

在此处输入图片说明

caching node.js typescript nestjs

10
推荐指数
1
解决办法
2042
查看次数

NestJS 控制器未映射

所以我有一个将部署在 docker 容器中的 API。这个 API 有authentications控制器,简单而不特别。

当我在本地机器上以开发模式启动 API 时,将找到 auth 控制器并且一切正常。在我的本地机器上构建和运行它也是如此。但是当我对项目进行 dockerize 并在虚拟机上运行它时,我将无法访问 auth 控制器。每个其他控制器都在工作,但 auth 控制器不存在。

查看 docker 日志,不会映射任何身份验证控制器。本地和构建的 docker 镜像都应该包含相同的项目文件。

身份验证控制器:

import {
  Controller,
  Post,
  Delete,
  UseGuards,
  Request,
  Body,
} from '@nestjs/common';

import { AuthenticationsService } from './authentications.service';
import { JwtAuthGuard } from '../shared/guards/jwtAuth.guard';
import { SignInDTO } from './dtos/addGraphNodeToGraphByGraphId.dto';

@Controller('authentications')
export class AuthenticationsController {
  constructor(
    private readonly authenticationsService: AuthenticationsService,
  ) {}

  @Post()
  public signIn(@Body() { username, password }: SignInDTO): Promise<string> {
    return this.authenticationsService.signIn(username, password);
  } …
Run Code Online (Sandbox Code Playgroud)

javascript docker typeorm nestjs

10
推荐指数
3
解决办法
2150
查看次数

EntityMetadataNotFound:找不到“任务”的元数据 - NestJS

我正在从 Udemy https://www.udemy.com/course/nestjs-zero-to-hero学习 NestJS 课程。

而且我遇到了一个奇怪的问题,我尝试了很多东西,但似乎没有任何效果。这是我拥有的问题和完整代码。

我得到的错误

我得到的错误

我的 ORM 配置文件:

ORM 配置文件

**任务实体文件:** 在此处输入图片说明

最后我在 tasks.module.ts 文件中导入配置文件 在此处输入图片说明

面临这个问题的人已经用不同的修复方法解决了这个问题,

  1. 有人说我们可能在配置中添加了拼写错误的文件名或路径,这可能会导致此问题。
  2. 有人说从 npm 改为 yarn 已经解决了这个问题。
  3. 也很少有人说问题出在 ORM 本身。

我已经尝试了所有可以通过 Internet 获得的可能解决方案,但无法解决此问题。现在已经好几天了,我正在寻找堆栈溢出的帮手或救星。

同时,我将尝试查看更多可能有帮助的可能性,但如果您遇到此问题,请告诉我可能的解决方案。

javascript orm node.js typeorm nestjs

10
推荐指数
4
解决办法
4409
查看次数

如何修复socket io中的400错误错误请求?

我有一个前端应用程序(VUE JS)

我有一个后端(Nest JS)

Vue JS 应用程序使用 vue-socket.io-extended 库通过 websockets 从后端获取数据当 Vue JS 应用程序启动时,我在浏览器中看到错误:

polling-xhr.js?d33e:229 POST http://localhost:11050/socket.io/?EIO=4&transport=polling&t=NMXgCF1 400(错误请求)

错误

我该如何解决这个错误?

我认为它没有与库连接,我只是尝试了 socket io 库,结果是一样的。

服务器正在工作,因为它发送日志并显示谁已连接:

服务器响应

服务器(Nest JS) main.ts 文件:

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableCors();
  await app.listen(11050);
}
bootstrap();
Run Code Online (Sandbox Code Playgroud)

应用网关:

@WebSocketGateway()
export class AppGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {


  private logger: Logger = new Logger('AppGatway');

  @SubscribeMessage('msgToServer')
  handleMessage(client: Socket, text: string): WsResponse<string> {
    return { event: 'msgToClient', data: text };
  }

  afterInit(server: Server) {
    this.logger.log('Initialised!');
  }

  handleConnection(client: Socket, …
Run Code Online (Sandbox Code Playgroud)

javascript websocket socket.io vue.js nestjs

10
推荐指数
5
解决办法
9519
查看次数

类型错误:将循环结构转换为 JSON --&gt; 从构造函数“ClientRequest”的对象开始

我是 nest.js 初学者,我正在尝试用我的代码实现 Axios,但出现此错误,我想修复它。

    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'Socket'
    --- property '_httpMessage' closes the circle +188941ms
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'Socket'
    --- property '_httpMessage' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (D:\CUSportcomplex-register\sso-reg\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (D:\CUSportcomplex-register\sso-reg\node_modules\express\lib\response.js:260:14)
    at ExpressAdapter.reply (D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\platform-express\adapters\express-adapter.js:24:57)
    at RouterResponseController.apply (D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-response-controller.js:13:36)
    at D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-execution-context.js:173:48
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-execution-context.js:47:13
    at async D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-proxy.js:9:17
Run Code Online (Sandbox Code Playgroud)

这是我的 …

typescript axios nestjs

10
推荐指数
6
解决办法
2万
查看次数