5

我想了解如何通过 DI 在 nestjs 中导入 3rd 方库。所以,我有一堂课AuthService

export class AuthService {
   constructor(
     @Inject(constants.JWT) private jsonWebToken: any,
   ){}
  ....
}

智威汤逊提供商:

import * as jwt from 'jsonwebtoken';
import {Module} from '@nestjs/common';
import constants from '../../../constants';

const jwtProvider = {
  provide: constants.JWT,
  useValue: jwt,
};

@Module({
  components: [jwtProvider],
})
export class JWTProvider {}

图书馆模块:

import { Module } from '@nestjs/common';
import {BcryptProvider} from './bcrypt/bcrypt.provider';
import {JWTProvider} from './jsonwebtoken/jwt.provider';

@Module({
  components: [
    BcryptProvider,
    JWTProvider,
  ],
  controllers: [],
  exports: [
    BcryptProvider,
    JWTProvider,
  ],
})
export class LibrariesModule{
}

我收到此错误:

Error: Nest can't resolve dependencies of the AuthService (?). Please verify whether [0] argument is available in the current context.
    at Injector.<anonymous> (D:\Learning\nest\project\node_modules\@nestjs\core\injector\injector.js:156:23)
    at Generator.next (<anonymous>)
    at fulfilled (D:\Learning\nest\project\node_modules\@nestjs\core\injector\injector.js:4:58)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

此外,我想听听一些关于不在变量中使用类型any的建议。jsonWebToken

4

1 回答 1

2

魔鬼在细节中。您可以像这样将其他模块“导入”到 AuthModule 中:

@Module({
  modules: [LibrariesModule], // <= added this line
  components: [AuthService, JwtStrategy],
  controllers: [],
})
export class AuthModule {

}

来源:这里

第二个问题仍然悬而未决。

于 2017-12-11T09:26:09.970 回答