我正在做一个 Nest.js 程序,但我找不到我的依赖问题。我搜索了很多,找到了很多关于这个问题的答案,但我不明白为什么我的代码不能\xc2\xb4t 工作。所以我有一个产品模块,其中有他的 DTO\xc2\xb4s、实体、控制器、服务和模块,此外它还有一个服务接口。
\n产品控制器
\nimport { Controller, Get } from '@nestjs/common';\nimport { ProductServiceInterface } from './interface/product.service.interface'\n\n@Controller('products')\nexport class ProductController {\n constructor(private readonly productService: ProductServiceInterface) {}\n\n @Get()\n getHello(): string {\n return this.productService.test();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n产品服务接口
\nimport { Injectable } from '@nestjs/common';\nimport {\n CreateProductInput,\n CreateProductOutput,\n} from '../dto/create-product.dto';\nimport { FindProductOutput } from '../dto/find-product.dto';\n\nexport interface ProductServiceInterface {\n create(input: CreateProductInput): Promise<CreateProductOutput>;\n findProduct(productId: string): Promise<FindProductOutput>;\n test();\n}\nRun Code Online (Sandbox Code Playgroud)\n产品服务
\nimport { Injectable } from '@nestjs/common';\nimport { ProductServiceInterface } from './interface/product.service.interface';\nimport {\n CreateProductInput,\n …Run Code Online (Sandbox Code Playgroud) 我在 NestJs 中遇到了循环依赖问题。错误:
Scope [AppModule -> PlanModule -> TaskModule] +17ms
Error: Nest cannot create the UserModule instance.
The module at index [1] of the UserModule "imports" array is undefined.
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
@Inject(forwardRef(() => TaskService))
private readonly tasksService: TaskService
Run Code Online (Sandbox Code Playgroud)
onModuleInit() {
this.userService = this.moduleRef.get(UserService);
}
Run Code Online (Sandbox Code Playgroud)
import { TaskModule } from '../task/task.module';
Run Code Online (Sandbox Code Playgroud)
用户模块
@Module({
providers: [UserService, UserResolver],
exports: [UserService],
imports: [JournalModule, PlanModule, TaskModule],
})
export class UserModule {}
Run Code Online (Sandbox Code Playgroud)
任务模块
@Module({
providers: [TaskService, TaskResolver],
exports: …Run Code Online (Sandbox Code Playgroud) 我使用缓存管理器作为内存中数据存储,如文档https://docs.nestjs.com/techniques/caching https://www.npmjs.com/package/cache-manager中所述
有没有可能的方法来检索所有数据作为示例
const value = this.cacheManager.getall();
Run Code Online (Sandbox Code Playgroud) 我正在尝试为 NestJS 创建一个“worker”,它基本上聚合来自多个数据源的数据。由于我将此工作程序部署到 Kubernetes 集群中,因此我不需要启动 NestJS 内部 HTTP 服务器,但是,调度程序不会在没有app.listen.
主要.ts:
async function bootstrap() {
const app = await NestFactory.create(WorkerModule);
await app.listen(3030); // <-- I would like to delete this
}
bootstrap();
Run Code Online (Sandbox Code Playgroud)
工作模块.ts
@Module({
imports: [
ScheduleModule.forRoot(),
],
providers: [
// Scrappers
DataScrapper,
],
})
export class WorkerModule {}
Run Code Online (Sandbox Code Playgroud)
数据抓取器.ts
@Injectable()
export class DataScrapper {
@Interval(1000)
async sync() {
console.log('fetching...');
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 a@SetMetaData('version', 'v2')为控制器中的 http 方法设置版本控制。然后我有一个自定义@Get()装饰器将版本作为后缀添加到控制器路由中。
/api/cats/v2/firstfive这样,当我有时,我就可以使用
@SetMetaData('version', 'v2')
@Get('firstfive')
Run Code Online (Sandbox Code Playgroud)
但我没有看到将 Reflector 注入到我的自定义 @Get 装饰器中的明确方法。
我的 Get 装饰器如下,
import { Get as _Get } from '@nestjs/common';
export function Get(path?: string) {
version = /*this.reflector.get('version') or something similar */
return applyDecorators(_Get(version+path));
}
Run Code Online (Sandbox Code Playgroud)
请帮帮我!谢谢!
我已经在 NestJS 后端中使用 PassportStrategy 实现了 Google 登录(NestJS 后端开发基于本指南: https: //medium.com/@nielsmeima/auth-in-nest-js-and-angular-463525b6e071)。
@Get('google')
@UseGuards(AuthGuard('google'))
googleLogin()
{
// initiates the Google OAuth2 login flow
}
@Get('google/callback')
@UseGuards(AuthGuard('google'))
googleLoginCallback(@Req() req, @Res() res)
{
// handles the Google OAuth2 callback
const jwt: string = req.user.jwt;
if (jwt)
return res.status(HttpStatus.OK).cookie('jwt', jwt, {
httpOnly: true
}).redirect('/');
else
res.redirect('http://localhost:4200/login/failure');
}
Run Code Online (Sandbox Code Playgroud)
使用 @UseGuards(AuthGuard('google')) 应该启动 google 登录:
super({
clientID : 'XXX', // <- Replace this with your client id
clientSecret: 'XXX', // <- Replace this with your client secret …Run Code Online (Sandbox Code Playgroud) 所以我刚刚开始从Express切换到使用NestJS。
在Express中,一切都是对象,无论是顶层应用程序、路由器、请求、响应等。
NestJS中的一切都几乎相同。它融合了Express所拥有的一切以及更多功能。
有一点我不明白为什么Nest中的一切都如此基于类?
为所有东西定义类有什么意义?比如控制器、模块(路由器)、中间件、中间件的使用者等等。
我非常有兴趣了解确切的哲学和设计模式。
有没有办法将生成的 swagger.json 作为集合导入到 Postman 中?
我尝试导入它,但端点未显示在 Postman 中?
我正在使用 NestJs 和 Swagger + Postman
我在 Nestjs 中有一个控制器来处理 HTTP 请求。有一个 IoT 设备与服务器进行通信,并通过 post 请求将最新更改发送到服务器。同时,有一个移动应用程序应该使用 Websockets 实时接收更新。
我知道 HTTP 请求和 Websockets 在 Nestjs 中是不同的概念,但是有没有办法在我收到 HTTP 请求时发出事件?
任何想法将不胜感激。
我不知道我错过了什么,但当我调试时,突变或查询的输入始终为空,似乎输入是空对象
这是我的代码:
解析器
@Query(() => String)
async testMutation(@Args('args') args: UpvotePostInput) {
return args.postId;
}
Run Code Online (Sandbox Code Playgroud)
数据传输工具
import { InputType, Field } from '@nestjs/graphql';
@InputType()
export class UpvotePostInput {
@Field(() => String)
postId: string;
}
Run Code Online (Sandbox Code Playgroud)
错误
{
"errors": [
{
"message": "Cannot return null for non-nullable field Query.testMutation.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"testMutation"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Cannot return null for non-nullable field Query.testMutation.",
" at completeValue (/....js:559:13)", …Run Code Online (Sandbox Code Playgroud) nestjs ×10
node.js ×5
javascript ×3
graphql ×2
typescript ×2
api ×1
express ×1
passport.js ×1
postman ×1
reactjs ×1
scheduler ×1
socket.io ×1
swagger ×1
typeorm ×1