node.js - مشكلة nestjs - لا يمكن لـ Nest حل تبعيات AuthenticationService (?)
يتوقف التطبيق فوراً عند بدء التشغيل، لم يتم فحص الوحدات الأخرى بعد، لذا من غير المرجح أن يكون الخطأ فيها.
authentication.service.ts
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { CreateUserDto } from 'src/users/dto/createUser.dto';
import { UserService } from 'src/users/user/user.service';
import bcrypt from 'bcrypt';
@Injectable()
export class AuthenticationService {
constructor(@Inject() private readonly userService: UserService){}
public async regiser(registartionData:CreateUserDto){
const hashingPassword = await bcrypt.hash(registartionData.passwordHash, 10);
try{
const createUser = await this.userService.create({
...registartionData,
passwordHash: hashingPassword
})
createUser.passwordHash = undefined;
return createUser;
}
catch(err){
throw new HttpException('Something went wrong', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
public async getAuthenticationUser(email:string, textPassword:string){
try{
const user = await this.userService.findByEmail(email);
await this.verifyPassword(textPassword,user.passwordHash);
user.passwordHash = undefined;
return user;
}
catch(err){
throw new HttpException('Wrong credentials provided', HttpStatus.BAD_REQUEST);
}
}
private async verifyPassword(password:string, hashingPassword:string){
const isMatching = await bcrypt.compare(password, hashingPassword);
if(!isMatching) throw new HttpException('Wrong credentials provided', HttpStatus.BAD_REQUEST);
}
}
auth.module.ts
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { UsersModule } from 'src/users/users.module';
import { AuthController } from '../controllers/auth/auth.controller';
import { AuthenticationService } from './authentication/authentication.service';
import { LocalStrategy } from './authentication/local.strategy';
@Module({
imports:[UsersModule, PassportModule, ],
providers: [AuthenticationService, LocalStrategy],
controllers:[AuthController],
exports: [AuthenticationService]
})
export class AuthModule {}
هذا الخطأ
Nest can't resolve dependencies of the AuthenticationService (?). Please make sure that the argument dependency at index [0] is available in the AuthModule context.
Potential solutions:
- If dependency is a provider, is it part of the current AuthModule?
- If dependency is exported from a separate @Module, is that module imported within AuthModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
لا أفهم الخطأ، قم بتصدير وتوفير خدمة المصادقة. أنا لا أستخدم خدمة المصادقة في الوحدات الأخرى.
الإجابة رقم 1
جميع خدماتي تستخدم نموذج MongoDB. لذلك، تحتاج إلى القيام بما يلي في الوحدات والخدمات.
user.module.ts
@Module({
*imports: [MongooseModule.forFeature([{ name: 'User', schema: UserSchema }])],*
providers: [UserService],
exports: [UserService],
})
export class UsersModule {}
user.service.ts
@Injectable()
export class UserService implements IUserService {
constructor(
*@InjectModel(User.name) private userModel: Model<User>*
) {}
مثال جيد https://wanago.io/2021/08/23/api-nestjs-relationships-mongodb/
الإجابة رقم 2
الكود في مستودعك لا يمكن حتى تجميعه، لذلك لا يمكنني مساعدتك تقريبًا على الإطلاق. هناك نقص في الأشياء، وأخطاء TypeScript، وما إلى ذلك.
لكن من ما أرى:
- وحدة UsersModule تحتوي على المزود
UserServiceلكنها لا تقوم بتصديره. لذلك لا يمكن لأي وحدة أخرى الوصول إليه. تحتاج أيضًا إلى تصدير UserService. - وحدة AuthModule لا تستورد UserService (ولا شيء يصدرها) لذا فهي غير موجودة في وقت محاولة Nest لمطابقة الحقن. ما يمكنك فعله هو إضافة
imports: [UserModule]في AuthModule (وأيضًا تصدير UserService في UserModule).
وقم بقراءة توثيق الوحدات ومشروع المثال مرة أخرى لمعرفة كيفية عمل الاستيراد، والتصدير، ووحدات التحكم، والموفرين.
