我正在尝试构建一个包含多个微服务的 NestJS 后端和一个作为与微服务通信的网关的 REST API。对于网关和微服务之间的通信,我使用 gRPC。简单的通信已经可以工作,但现在我想在微服务中实现错误处理。NestJS 文档指出,这可以通过 RpcException 类实现。https://docs.nestjs.com/microservices/exception-filters但是,如果我尝试捕获网关 API 中的异常,我只会收到“ERROR [ExceptionsHandler] 2 UNKNOWN:...”,然后是异常错误消息。
网关API:user.controller.ts
import { Controller, Get, Param } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { UserViewModel } from '../../proto/build/service-name/user';
import { UserService } from './user.service';
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get(':id')
async getUserById(@Param('id') id: number): Promise<UserViewModel> {
try {
return await this.userService.getUserById(id);
} catch (error) {
return error;
}
}
}
Run Code Online (Sandbox Code Playgroud)
网关API:user.service.ts
import { Inject, Injectable, OnModuleInit …Run Code Online (Sandbox Code Playgroud) 因此,我尝试使用 NestJS 创建我的第一个微服务,但当我尝试运行它时,服务因以下错误而停止:
[13:39:21] Found 0 errors. Watching for file changes.
Error: Cannot find module 'C:\Users\voryi\IdeaProjects\YWA\des_server\services\learning-service\dist\main'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
Run Code Online (Sandbox Code Playgroud) 在我们正在构建的项目中,工作流程中有一个标准的顺序。
我们如何正确测试事件是否已被触发但不实际执行其中的代码?
我们使用 Jest 进行测试。
用 Jest 进行模拟似乎不起作用,因为我们想要测试实际的触发事件而不是结果。
有什么建议么?
"node": "16.14.2"
"@nestjs/event-emitter": "^1.1.0",
"jest": "^27.5.1",
Run Code Online (Sandbox Code Playgroud) 我想使用 NestJS 框架。但更喜欢自己写SQL查询,而不是使用ORM的繁重接口。
我找到了一些教程,其中有直接访问数据库的示例,但它们没有使用NestJS的注入机制。因此,我正在尝试学习如何编写薄层控制器和服务提供程序,这使我可以自由地将查询作为文本参数提供。会感谢您的建议
我在 DTO 中使用 NestJS 和类转换器。
这是我所做的和我的问题的一个简单示例:
export class SomeDTO{
@Transform(({ value }) => value === "true" || value === true || value === 1)
@IsBoolean()
doDelete : boolean;
}
Run Code Online (Sandbox Code Playgroud)
我什至尝试过@Transform(({ value }) => { return value === "true" || value === true || value === 1})
现在,在我的控制器中:
@Post("something")
someOperation(@Body() data : SomeDTO){
console.log(data);
}
Run Code Online (Sandbox Code Playgroud)
记录数据时,预期的布尔值doDelete仍然是字符串,并且没有转换为其本机布尔类型。
是否尝试过提供这样的数据:
@Transform(({ value }) => { return false})
但在控制器中,如果我们将原始DTO doDelete设置为true,数据仍然是相同的。它没有像我们通过 暗示的那样转换为 false @Transform()。
我做错什么了吗?感谢您的帮助并提供了一些线索。
我已经尝试过这些相关参考文献,但似乎没有任何效果。
将 @nestjs/mongoose 从 9.0.3 更新到 9.1.0 后,我遇到以下错误:
[Nest] 3818 - 06/04/2022, 12:50:13 AM ERROR [ExceptionHandler] Nest can't resolve dependencies of the WatchlistService (?). Please make sure that the argument PortfolioModel at index [0] is available in the AppModule context.
Potential solutions:
- If PortfolioModel is a provider, is it part of the current AppModule?
- If PortfolioModel is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing PortfolioModel */ ]
}) …Run Code Online (Sandbox Code Playgroud) 我想在 NestJS 中使用 EJS 作为模板引擎。使用 Express,我可以在主文件中配置 EJS,如下所示:
app.set("view engine", "ejs");
Run Code Online (Sandbox Code Playgroud)
我怎样才能最好地用 NestJS 实现这个?Nestjs 不附带.set方法。
我无法让 NestJS 应用程序读取我的证书秘密文件,甚至是简单的 txt 文件。我收到错误:
错误 [ExceptionsHandler] ENOENT:没有这样的文件或目录
以下是我正在做的所有事情,使用 fs 读取文件:
import {FireblocksSDK, PeerType, TransactionArguments, TransactionOperation, TransactionStatus} from "fireblocks-sdk";
import fs = require('fs');
import path = require('path');
function fireblocks() {
const text = fs.readFileSync(('./data.txt'), 'utf8');
console.log(text);
const apiSecret = fs.readFileSync(path.resolve(__dirname, "../../certs/fireblocks_secret.key"), "utf8");
return new FireblocksSDK(apiSecret, process.env.FIREBLOCKS_ACCESS_TOKEN);
}
Run Code Online (Sandbox Code Playgroud)
以下是我正在使用的文件结构:
错误 [ExceptionsHandler] ENOENT:没有这样的文件或目录
我正在用 Nest 构建一个 API。对于 E2E 测试,我使用 Jest,在 beforeAll() 中,我使用 AppModule 中的 createNestApplication 实例化一个完整的 NestJS 应用程序:
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = module.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
);
await app.init();
Run Code Online (Sandbox Code Playgroud)
测试和验证是使用应用程序对象进行的。对于某些验证,以及在测试后从数据库中删除记录,我正在寻找一种方法从应用程序对象获取我的实体的存储库,或者至少是与数据库连接的 TypeOrm 实例。
查看该对象及其文档,我发现 NestJS 提供了一些从该对象中提取模块或提供程序的方法,但我还没有看到任何提取数据库连接实例的方法。我尝试使用这样的select 方法提取 TypeOrm 模块:
const typeOrmModule = app.select(TypeOrmModule);
Run Code Online (Sandbox Code Playgroud)
但这会导致错误:Nest 无法选择给定模块(当前上下文中不存在)
我不想在知道应用程序对象已经有一个连接的情况下生成与数据库的新连接。所以我想知道是否有一种方法可以提取该连接的实例或该对象内部的存储库。提前致谢
我正在尝试模拟一个存储库。我不想进行实际的数据库调用。我(认为我)正在关注 NestJS 的文档以及某些 stackoverflow 项目。
\n但是,当我运行测试时,出现以下错误:
\nJwtStrategy \xe2\x80\xba validate \xe2\x80\xba throws an unauthorized exception as user cannot be found\nNest can\'t resolve dependencies of the UserEntityRepository (?). Please make sure that the argument DataSource at index [0] is available in the TypeOrmModule context.\n\nPotential solutions:\n- If DataSource is a provider, is it part of the current TypeOrmModule?\n- If DataSource is exported from a separate @Module, is that module imported within TypeOrmModule?\n @Module({\n imports: [ /* the Module containing DataSource */ …Run Code Online (Sandbox Code Playgroud) nestjs ×10
node.js ×4
typescript ×4
typeorm ×3
jestjs ×2
database ×1
ejs ×1
eventemitter ×1
express ×1
grpc ×1
javascript ×1
module ×1
mongoose ×1