我正在一个项目中使用 NestJs 框架。今天发现EsLint发现了587个错误的问题。
warning 'IsBoolean' is defined but never used @typescript-eslint/no-unused-vars
warning 'IsEmail' is defined but never used @typescript-eslint/no-unused-vars
warning 'IsEnum' is defined but never used @typescript-eslint/no-unused-vars
warning 'IsInt' is defined but never used @typescript-eslint/no-unused-vars
warning 'IsOptional' is defined but never used @typescript-eslint/no-unused-vars
...
Run Code Online (Sandbox Code Playgroud)
error No named exports found in module './users.controller' import/export
error No named exports found in module './users.service' import/export
error No named exports found in module './users.module'
Run Code Online (Sandbox Code Playgroud)
但使用了所有装饰器,并且没有索引文件具有命名导出。这是我的 DTO 类的示例
import {
IsBoolean,
IsEmail,
IsEnum, …Run Code Online (Sandbox Code Playgroud) 这个简单的演示有一个错误 https://docs.nestjs.com/techniques/http-module
import { Get, Controller, HttpService } from '@nestjs/common';
import { AxiosResponse } from 'axios'
import { Observable } from 'rxjs'
@Controller()
export class AppController {
constructor(private readonly http: HttpService) {}
@Get()
root(): Observable<AxiosResponse<any>> {
return this.http.get('https://api.github.com/users/januwA');
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
[Nest] 7356 - 2018-10-18 00:08:59 [ExceptionsHandler] Converting circular structure to JSON +9852ms
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
Run Code Online (Sandbox Code Playgroud)
nest i
common version : 5.1.0
core version : 5.1.0
Run Code Online (Sandbox Code Playgroud) 我正在使用Bull实现 NestJS 工作线程、队列。
根据文档,工作程序和服务器(将)在同一个“进程”中运行,但我想在单独的进程中运行工作程序,以免阻塞主事件循环。
我认为这被称为“在单独的二进制文件中运行任务”或其他名称。
无论如何,我尝试用谷歌搜索它,浏览 NestJS 的文档,但找不到类似的东西。
++ 换句话说:
我有一个主项目(我当前的),我想在单独的进程(独立应用程序)中创建工作人员,并希望连接我当前的主项目和工作人员。而且我在文档中找不到它。
我应该在哪个模块中实例化我的 Bull 实例?我假设我会将其保留producer在主模块和consumer工作模块中。
我怎样才能这样做呢?
请注意,我所说的“单独进程”并不是指在单独进程中运行特定任务,如 Bull文档中所定义。我想将整个工作模块部署在一个单独的进程中或任何应该使用的术语中。
++ [额外,如果可能的话]
在运行我的服务器和worker之前,我还想检查我的worker(公牛实例)是否成功连接到我的Redis服务器。我在公牛的文档中找不到任何内容...您认为有一个好的解决方法吗?
这里的文档有点薄,所以我遇到了一个问题。我尝试使用 Guards 来保护 Controller 或它的 Actions,所以我会要求经过身份验证的请求的作用(通过 JWT)。在我的 auth.guard.ts 中,我要求“request.user”但它是空的,所以我无法检查用户角色。我不知道如何定义“request.user”。这是我的 auth 模块和它的导入。
auth.controller.ts
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { RolesGuard } from './auth.guard';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Get('token')
async createToken(): Promise<any> {
return await this.authService.signIn();
}
@Get('data')
@UseGuards(RolesGuard)
findAll() {
return { message: 'authed!' };
}
}
Run Code Online (Sandbox Code Playgroud)
角色.guard.ts
这里 user.request 是空的,因为我从来没有定义过它。文档没有显示如何或在哪里。
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; …Run Code Online (Sandbox Code Playgroud) 我正在使用 NestJS 内置记录器。我找不到设置全局日志级别的选项。例如,开发环境的日志级别为“详细”,生产环境的日志级别为“错误”。
另外,是否有一个选项可以禁用 NestJS 在输出中添加的内容(即时间戳和其他信息)并仅记录消息?我需要这个,因为我们使用 Kibana 日志,它会自动添加所需的字段。
我想使用环境变量来配置HttpModule每个模块,从文档中我可以使用这样的配置:
@Module({
imports: [HttpModule.register({
timeout: 5000,
maxRedirects: 5,
})],
})
Run Code Online (Sandbox Code Playgroud)
但我不知道从环境变量(或配置服务)中包含 baseURL 的最佳实践是什么,例如:
@Module({
imports: [HttpModule.register({
baseURL: this.config.get('API_BASE_URL'),
timeout: 5000,
maxRedirects: 5,
})],
Run Code Online (Sandbox Code Playgroud)
这this.config是undefined因为它不在课堂上。
从环境变量(或配置服务)设置 baseURL 的最佳实践是什么?
我正在开发一个 Nest.js 服务器,并希望能够在控制台(例如 console.log)中打印有用的堆栈跟踪。默认情况下,它返回对编译源 (.js) 中行号的引用。这对调试没有用,因为它缺少对原始源文件 (.ts) 中行号的引用
这是我的 tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"_baseUrl": "./",
"incremental": true
},
"exclude": ["node_modules", "dist"]
}
Run Code Online (Sandbox Code Playgroud)
.map 文件也在 dist 文件夹中生成,尽管在控制台中检查堆栈跟踪时它似乎没有用。
我将 @UseInterceptors 与 @nestjs/platform-express 中的 FilesInterceptor 一起使用。FilesInterceptor 允许我传递一些配置(例如文件存储文件夹)。我想传递的不是精确值(例如“public”),而是从 configService 获取它们。
从这个答案(/sf/answers/4187966611/)我明白为什么从 UseInterceptors 调用 this.configService 会导致未定义。
@UseInterceptors(FilesInterceptor('cat_pic', 2, this.configService.get('storageConfigs')))
Run Code Online (Sandbox Code Playgroud)
那么如何将配置传递给 FilesInterceptor 呢?或者,如果我将文件拦截器功能移至单独的模块,如何访问服务?
我想了解nest js动态模块中forRoot和forFeature之间的区别。
我还想了解 TypeOrm 动态模块与 Nestjs 一起使用的情况下的差异。
我有 Mongo 用户集合和地址集合。每个地址都由一个用户拥有。这是我的架构类:
export type UserDocument = User & mongoose.Document
@Schema({ timestamps: true })
export class User {
// @Prop({ type: mongoose.Types.ObjectId })
_id: string
@Prop({ required: true })
name: string
@Prop({ required: true, unique: true })
email: string
@Prop({ select: false })
password: string
}
export const UserSchema = SchemaFactory.createForClass(User)
export type AddressDocument = Address & Document
@Schema({ timestamps: true })
export class Address {
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: User.name })
user: User
@Prop()
line1: string
@Prop()
line2?: string …Run Code Online (Sandbox Code Playgroud) nestjs ×10
typescript ×5
javascript ×4
node.js ×4
axios ×1
console.log ×1
decorator ×1
eslint ×1
logging ×1
mongoose ×1
stack-trace ×1
typeorm ×1
worker ×1