Nest js Внедрение зависимостей зависимостей не работает - PullRequest
0 голосов
/ 01 апреля 2020

Я использую sequelize ORM с гнездом js. Я создал новую модель «контакт» и хочу внедрить сервис внутри. Когда я делаю это, у меня появляется ошибка в консоли. Я думаю, что понимаю, почему я не могу, но я не могу найти решение. Почему это невозможно сделать?

Спасибо!

contact.entity.ts

@ObjectType()
@Table
export class Contact extends Model<Contact> {

  constructor(private utilsService: UtilsServices) {
    super();
  }

  @Field(type => Int, { nullable: true })
  @PrimaryKey
  @AutoIncrement
  @Column
  id?: number;

  @Field({ nullable: true })
  @Column
  get firstName(): string {
    return this.utilsService.decrypt(this.getDataValue('firstName'));
  }

  set firstName(value) {
    const encryptValue = this.utilsService.encrypt(value);
    this.setDataValue('firstName', encryptValue);
  }

  @Field({ nullable: true })
  @Column
  get lastName(): string {
    return this.utilsService.decrypt(this.getDataValue('lastName'));
  }

contacts.module. ts

import { Module } from '@nestjs/common';
import { UtilsModule } from '../utils/utils.module';
import { DatabaseModule } from '../database/database.module';
import { WinstonModule } from 'nest-winston';
import { contactsProviders } from './providers/contacts.providers';

@Module({
  imports: [
    UtilsModule,
    DatabaseModule,
    WinstonModule
  ],
  providers: [
    ...contactsProviders
  ]
})
export class ContactsModule {}

utils.service.ts

import { Inject, Injectable } from '@nestjs/common';
import { Logger } from 'winston';
import { ConfigService } from '@nestjs/config';
import * as CryptoJS from 'crypto-js';

@Injectable()
export class UtilsServices {

  constructor(@Inject('winston') private readonly logger: Logger,
              private readonly configService: ConfigService) {
  }

  /**
   * Encrypt data with a secret key store in env.
   * @param {string} data - A string to encrypt
   * @returns {Promise<string>} Encrypted data
   */
  encrypt(data): string {

    if (data === null || data === undefined || data === '') {
      return data;
    }
    const secretKey = this.configService.get('encrypt_secret');
    return CryptoJS.AES.encrypt(data, secretKey).toString();
  }

  /**
   * Decrypt data with a secret key store in env
   * @param {string} data - A string to encrypt
   * @returns {Promise<string>} Decrypted data
   */
  decrypt(data): string {
    if (data === null || data === undefined || data === '') {
      return data;
    }
    const secretKey = this.configService.get('encrypt_secret');
    const bytes = CryptoJS.AES.decrypt(data, secretKey);
    return bytes.toString(CryptoJS.enc.Utf8);
  }
}

Мое сообщение об ошибке:


src/database/database.providers.ts:41:9 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Type 'typeof Contact' is not assignable to type 'string | ModelCtor<Model<any, any>>'.
      Type 'typeof Contact' is not assignable to type 'ModelCtor<Model<any, any>>'.
        Type 'typeof Contact' is not assignable to type 'new () => Model<any, any>'.

41         Contact,
           ~~~~~~~

  node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.d.ts:16:5
    16     addModels(arg: Array<ModelCtor | string>): any;
           ~~~~~~~~~
    The last overload is declared here.


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...