修复 "Nest can't resolve dependencies of the MovieService."
Fix for "Nest can't resolve dependencies of the MovieService."
我一直收到此错误,但不知道为什么。如果有人能帮我解决它,那会让我开心
Error: Nest can't resolve dependencies of the MovieService (?). Please make sure that the argument MovieRepository at index [0] is available in the MovieService context.
这是我的 MovieService 的结构:
import {Injectable} from "@nestjs/common";
import {InjectRepository} from "@nestjs/typeorm";
import {Repository} from "typeorm";
import {CreateMovieDto} from "./dto/create-movie.dto";
import {Movie} from "./movie.entity";
@Injectable()
export class MovieService {
constructor(
@InjectRepository(Movie)
private readonly movieRepository: Repository<Movie>
) {}
create(createMovieDto: CreateMovieDto): Promise<Movie> {
const movie = new Movie();
movie.path = createMovieDto.path;
movie.title = createMovieDto.title;
movie.originalTitle = createMovieDto.originalTitle;
movie.poster = createMovieDto.poster;
movie.descr = createMovieDto.descr;
movie.year = createMovieDto.year;
return this.movieRepository.save(movie);
}
async findAll(): Promise<Movie[]> {
return this.movieRepository.find();
}
findOne(id: string): Promise<Movie> {
return this.movieRepository.findOne(id);
}
findByPath(path: string): Promise<Movie> {
return this.movieRepository.findOne({path});
}
async remove(id: string): Promise<void> {
await this.movieRepository.delete(id);
}
}
电影模块:
import {Module} from "@nestjs/common";
import {TypeOrmModule} from "@nestjs/typeorm";
import {Movie} from "./movie.entity";
import {MovieService} from "./movie.service";
@Module({
imports: [TypeOrmModule.forFeature([Movie])],
providers: [MovieService],
exports: [MovieService],
})
export class MovieModule {}
电影实体:
import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";
@Entity()
export class Movie {
@PrimaryGeneratedColumn()
id: number;
@Column()
path: string;
@Column()
title: string;
@Column()
originalTitle: string;
@Column()
poster: string;
@Column()
descr: string;
@Column()
year: string;
}
和应用程序模块:
import {Module} from "@nestjs/common";
import {TypeOrmModule} from "@nestjs/typeorm";
import {AppController} from "./app.controller";
import {AppService} from "./app.service";
import {MovieModule} from "./movie/movie.module";
@Module({
imports: [
TypeOrmModule.forRoot({
type: "mysql",
host: "localhost",
port: 3306,
username: "*****",
password: "*****",
database: "*****",
autoLoadEntities: true,
synchronize: true,
}),
MovieModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
编辑 1
我尝试将电影文件夹复制粘贴到一个新的嵌套项目中,并且成功了。我对为什么会收到此错误感到更加困惑
这是我的 package.json
{
"name": "marineris",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/typeorm": "^8.0.2",
"bcrypt": "^5.0.1",
"cookie-parser": "^1.4.5",
"hbs": "^4.1.2",
"mysql2": "^2.3.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"sync-request": "^6.1.0",
"typeorm": "^0.2.38",
"uuidv4": "^6.2.12"
},
"devDependencies": {
"@nestjs/cli": "^8.0.0",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@types/cookie-parser": "^1.4.2",
"@types/express": "^4.17.13",
"@types/jest": "^27.0.1",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\.spec\.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
我解决了我的问题。我在项目的一些文件中弄乱了我的 imports/exports,在仔细检查它们后它起作用了。
我一直收到此错误,但不知道为什么。如果有人能帮我解决它,那会让我开心
Error: Nest can't resolve dependencies of the MovieService (?). Please make sure that the argument MovieRepository at index [0] is available in the MovieService context.
这是我的 MovieService 的结构:
import {Injectable} from "@nestjs/common";
import {InjectRepository} from "@nestjs/typeorm";
import {Repository} from "typeorm";
import {CreateMovieDto} from "./dto/create-movie.dto";
import {Movie} from "./movie.entity";
@Injectable()
export class MovieService {
constructor(
@InjectRepository(Movie)
private readonly movieRepository: Repository<Movie>
) {}
create(createMovieDto: CreateMovieDto): Promise<Movie> {
const movie = new Movie();
movie.path = createMovieDto.path;
movie.title = createMovieDto.title;
movie.originalTitle = createMovieDto.originalTitle;
movie.poster = createMovieDto.poster;
movie.descr = createMovieDto.descr;
movie.year = createMovieDto.year;
return this.movieRepository.save(movie);
}
async findAll(): Promise<Movie[]> {
return this.movieRepository.find();
}
findOne(id: string): Promise<Movie> {
return this.movieRepository.findOne(id);
}
findByPath(path: string): Promise<Movie> {
return this.movieRepository.findOne({path});
}
async remove(id: string): Promise<void> {
await this.movieRepository.delete(id);
}
}
电影模块:
import {Module} from "@nestjs/common";
import {TypeOrmModule} from "@nestjs/typeorm";
import {Movie} from "./movie.entity";
import {MovieService} from "./movie.service";
@Module({
imports: [TypeOrmModule.forFeature([Movie])],
providers: [MovieService],
exports: [MovieService],
})
export class MovieModule {}
电影实体:
import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";
@Entity()
export class Movie {
@PrimaryGeneratedColumn()
id: number;
@Column()
path: string;
@Column()
title: string;
@Column()
originalTitle: string;
@Column()
poster: string;
@Column()
descr: string;
@Column()
year: string;
}
和应用程序模块:
import {Module} from "@nestjs/common";
import {TypeOrmModule} from "@nestjs/typeorm";
import {AppController} from "./app.controller";
import {AppService} from "./app.service";
import {MovieModule} from "./movie/movie.module";
@Module({
imports: [
TypeOrmModule.forRoot({
type: "mysql",
host: "localhost",
port: 3306,
username: "*****",
password: "*****",
database: "*****",
autoLoadEntities: true,
synchronize: true,
}),
MovieModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
编辑 1
我尝试将电影文件夹复制粘贴到一个新的嵌套项目中,并且成功了。我对为什么会收到此错误感到更加困惑
这是我的 package.json
{
"name": "marineris",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/typeorm": "^8.0.2",
"bcrypt": "^5.0.1",
"cookie-parser": "^1.4.5",
"hbs": "^4.1.2",
"mysql2": "^2.3.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"sync-request": "^6.1.0",
"typeorm": "^0.2.38",
"uuidv4": "^6.2.12"
},
"devDependencies": {
"@nestjs/cli": "^8.0.0",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@types/cookie-parser": "^1.4.2",
"@types/express": "^4.17.13",
"@types/jest": "^27.0.1",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\.spec\.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
我解决了我的问题。我在项目的一些文件中弄乱了我的 imports/exports,在仔细检查它们后它起作用了。