简体   繁体   English

Nest.js 无法解析依赖,找不到我的错误

[英]Nest.js cant resolve dependencies, can't find my mistake

I'm doing a Nest.js program but I can't find my dependencies problem.我正在做一个 Nest.js 程序,但我找不到我的依赖问题。 I have searched quite a lot and I have found a lot of answers regarding this problem, but I can't figure out why my code isn´t working.我进行了很多搜索,并找到了很多有关此问题的答案,但我无法弄清楚为什么我的代码不起作用。 So I have a product module which has his DTO´s, Entity, Controller, Service and module, besides it has an interface for its service.所以我有一个产品模块,它有他的 DTO、实体、Controller、服务和模块,此外它还有一个服务接口。

ProductController产品控制器

import { Controller, Get } from '@nestjs/common';
import { ProductServiceInterface } from './interface/product.service.interface'

@Controller('products')
export class ProductController {
  constructor(private readonly productService: ProductServiceInterface) {}

  @Get()
  getHello(): string {
    return this.productService.test();
  }
}

ProductServiceInterface产品服务接口

import { Injectable } from '@nestjs/common';
import {
  CreateProductInput,
  CreateProductOutput,
} from '../dto/create-product.dto';
import { FindProductOutput } from '../dto/find-product.dto';

export interface ProductServiceInterface {
  create(input: CreateProductInput): Promise<CreateProductOutput>;
  findProduct(productId: string): Promise<FindProductOutput>;
  test();
}

ProductService产品服务

import { Injectable } from '@nestjs/common';
import { ProductServiceInterface } from './interface/product.service.interface';
import {
  CreateProductInput,
  CreateProductOutput,
} from './dto/create-product.dto';
import { Product } from './entity/product.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FindProductOutput } from './dto/find-product.dto';
//import WooCommerce from '../../config/woocomerce.config';

@Injectable()
export class ProductService implements ProductServiceInterface {
  constructor(
    @InjectRepository(Product)
    private productRepository: Repository<Product>,
  ) {}

  public async create(
    productDto: CreateProductInput,
  ): Promise<CreateProductOutput> {
    const product = new Product();
    product.name = productDto.name;
    product.price = productDto.price;
    product.imgUrl = productDto.imgUrl;

    const savedProduct = await this.productRepository.save(product);
    const productOutput = new CreateProductOutput();
    productOutput.id = savedProduct.id;
    return productOutput;
  }

  public async findProduct(idProduct: string): Promise<FindProductOutput> {
    const fetchedProduct = await this.productRepository.findOne(idProduct);
    const productOutput = new FindProductOutput();
    productOutput.id = fetchedProduct.id;
    productOutput.imgUrl = fetchedProduct.imgUrl;
    productOutput.name = fetchedProduct.name;
    productOutput.price = fetchedProduct.price;
    return productOutput;
  }

  public test() {
    return 'test'
    // WooCommerce.get('products', {
    //   pero_page: 20,
    // }).then((resp) => {
    //   return resp;
    // });
  }
}

ProductModule产品模块

import { ProductController } from './product.controller';
import { ProductService } from './product.service';
import { Product } from './entity/product.entity';

import { TypeOrmModule } from '@nestjs/typeorm';
import { Module } from '@nestjs/common';

@Module({
  imports: [TypeOrmModule.forFeature([Product])],
  controllers: [ProductController],
  providers: [ProductService],
})
export class ProductModule {}

AppModule应用模块

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import configuration from 'src/config/configuration';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TypeOrmConfigService } from 'src/config/typeorm.config.service';
import { ProductModule } from './modules/product/product.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [configuration],
      isGlobal: true,
    }),
    TypeOrmModule.forRootAsync({
      useClass: TypeOrmConfigService,
    }),
    ProductModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

I hope this code is enough for knowing where my mistake is, I don't like letting others just resolve this for me but I've been watching my code for hours and can't know how to resolve this dependencies problem.我希望这段代码足以知道我的错误在哪里,我不喜欢让其他人为我解决这个问题,但我已经看了好几个小时的代码,不知道如何解决这个依赖问题。

Interfaces disappear at runtime and becomes {} or Object .接口在运行时消失并变为{}Object Nest uses the parameter type metadata to determine what is supposed to be injected (usually via ClassType.name ). Nest 使用参数类型元数据来确定应该注入的内容(通常通过ClassType.name )。 You have two options to solve this你有两种选择来解决这个问题

  1. Use an abstract class instead of an interface.使用抽象 class 而不是接口。 This makes the class still visible at runtime so ClassType.name still works.这使得 class 在运行时仍然可见,因此ClassType.name仍然有效。

  2. Use @Inject('CustomToken') as the way to set the metadata for what Nest needs to inject.使用@Inject('CustomToken')作为设置 Nest 需要注入的元数据的方式。 You then need to make sure register the custom provider using something like然后,您需要确保使用类似的东西注册自定义提供程序

{
  provide: 'CustomToken',
  useClass: ClassToBeUsed
}

Either of these methods should fix your issue.这些方法中的任何一种都应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM