Nestjs throws a TS2322 err using the examples - node.js

I'm new to nest and I have run into an error which is not really decodable for me. I tried to follow the nest example, but no luck.
[tsl] ERROR in /Users/user/Documents/development/sprinta/src/server/app.module.ts(12,5)
[0] TS2322: Type '(DynamicModule | typeof ProjectModule)[]' is not assignable to type 'Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference<any>'.
[0] Type '(DynamicModule | typeof ProjectModule)[]' is not assignable to type 'ForwardReference<any>'.
[0] Property 'forwardRef' is missing in type '(DynamicModule | typeof ProjectModule)[]'.
app.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { AngularUniversalModule } from './modules/angular-universal/angular-universal.module';
import { JiraService } from './services/jira.service'
// modules
import { ProjectModule } from './resources/local/projects/project.module'
// sync
import {ProjectsSync} from './sync/projects.sync'
#Module({
imports: [
[TypeOrmModule.forRoot(), ProjectModule],
AngularUniversalModule.forRoot(),
],
controllers: [],
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}
project.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { ProjectService } from './project.service';
import { ProjectController } from './project.controller';
import { Project } from './project.entity';
#Module({
imports: [TypeOrmModule.forFeature([Project])],
providers: [ProjectService],
controllers: [ProjectController],
})
export class ProjectModule {}
project.service.ts
import { Injectable, Inject } from '#nestjs/common';
import { InjectRepository } from '#nestjs/typeorm';
import { Repository } from 'typeorm';
import { Project } from './project.entity';
#Injectable()
export class ProjectService {
constructor(
#InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
) {}
async findAll(): Promise<Project[]> {
return await this.projectRepository.find();
}
}
project.entity.ts
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
#Entity()
export class Project {
#PrimaryGeneratedColumn()
id: number;
#Column({ length: 500 })
name: string;
}
project.controller.ts
import { Controller, Get } from '#nestjs/common';
import { ProjectService } from './project.service';
import { Project } from './project.entity';
#Controller('project')
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}
#Get()
findAll(): Promise<Project[]> {
return this.projectService.findAll();
}
}
Thanks for the help

You have an extra nested array around TypeOrmModule.forRoot() and ProjectModule. The #Module call should read:
#Module({
imports: [
TypeOrmModule.forRoot(), ProjectModule,
AngularUniversalModule.forRoot(),
],
controllers: [],
providers:[JiraService, ProjectsSync],
})

Related

Nest can't resolve dependencies of the movieModel (?). Please make sure that the argument DatabaseConnection at index [0] is available i

I have trouble with using mongoose in nest
error message is below :
Nest can't resolve dependencies of the movieModel (?). Please make sure that the argument DatabaseConnection at index [0] is available in the MongooseModule context.
Potential solutions:
If DatabaseConnection is a provider, is it part of the current MongooseModule?
If DatabaseConnection is exported from a separate #Module, is that module imported within MongooseModule?
movies.service.ts
import { Injectable } from '#nestjs/common';
import { CreateMovieDto } from './dto/create-movie.dto';
import { InjectModel } from '#nestjs/mongoose';
import { movie, movieDocument } from './schema/movie.schema';
import { Model } from 'mongoose';
#Injectable()
export class MoviesService {
constructor(
#InjectModel(movie.name) private movieModel: Model<movieDocument>,
) {}
async create(CreateMovieDto: CreateMovieDto): Promise<movie> {
return new this.movieModel(CreateMovieDto).save();
}
async getAll(): Promise<movie[]> {
return this.movieModel.find().exec();
}
app.modules.ts
import { Module } from '#nestjs/common';
import { MongooseModule } from '#nestjs/mongoose';
import { MoviesModule } from './movies/movies.module';
#Module({
imports: [
MoviesModule,
MongooseModule.forRoot('mongodb://localhost:27017/databasename', {
connectionName: 'movies',
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
movie.module.ts
import { Module } from '#nestjs/common';
import { MongooseModule } from '#nestjs/mongoose';
import { MoviesController } from './movies.controller';
import { MoviesService } from './movies.service';
import { movieSchema } from './schema/movie.schema';
#Module({
imports: [
MongooseModule.forFeature([
{
name: 'movie',
schema: movieSchema,
collection: 'movies',
},
]),
],
controllers: [MoviesController],
providers: [MoviesService], //nestjs가 movieservice를 import하고 controller에 inject 한다.
})
export class MoviesModule {}
please help me figure this out

Nest can't resolve dependencies of the xService

error (cont): Please make sure that the argument PrismaService at index [0] is available in the Assoc_Page_TagModule context.
There are no errors in the code.
When I attempt > npm run start:dev
I initially get: Found 0 errors. Watching for file changes.
Then an error:
[ExceptionHandler] Nest can't resolve dependencies of the Assoc_Page_TagService (?). Please make sure that the argument PrismaService at index [0] is available in the Assoc_Page_TagModule context.
What is the argument at index [0]? Why is it expecting an argument?
Assoc_Page_Tag.service.ts
import { Injectable } from '#nestjs/common';
import { PrismaService } from '../database';
import { Prisma } from '#prisma/client';
#Injectable()
export class Assoc_Page_TagService {
constructor(private readonly prisma: PrismaService) {}
Assoc_Page_TagsAll() {
return Promise.all([
this.prisma.assoc_Page_Tag.findMany()]).then(([records, total]) => {
return {
records,
metadata: { total },
};
});
}
Assoc_Page_Tag.module.ts
import { Module } from '#nestjs/common';
import { Assoc_Page_TagController } from './Assoc_Page_Tag.controller';
import { Assoc_Page_TagService } from './Assoc_Page_Tag.service';
#Module({
imports: [],
controllers: [Assoc_Page_TagController],
providers: [Assoc_Page_TagService],
exports: [Assoc_Page_TagService],
})
export class Assoc_Page_TagModule {}
Assoc_Page_Tag.controller.ts
import {
Body,Controller,Get,Param,ParseIntPipe,Put,
} from '#nestjs/common';
import { Assoc_Page_TagService } from './Assoc_Page_Tag.service';
import { RESOURCE_BASE_ROUTE } from '../constant';
import { Prisma } from '#prisma/client';
const Route = RESOURCE_BASE_ROUTE.assoc_Page_Tag
#Controller()
export class Assoc_Page_TagController {
constructor(private readonly assoc_page_tagService: Assoc_Page_TagService) {}
#Get(`${Route}`)
all() {
return this.assoc_page_tagService.Assoc_Page_TagsAll();
}
}
app.module.ts
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Assoc_Page_TagModule } from './Assoc_Page_Tag';
#Module({
imports: [
Assoc_Page_TagModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Missing reference to PrismaModule - which exports PrismaService
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaModule } from './database';
import { Assoc_Page_TagModule } from './Assoc_Page_Tag';
#Module({
imports: [
PrismaModule,
Assoc_Page_TagModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

nest js : Error: Nest can't resolve dependencies of the PlanningService (?)

i'am facing this error when importing planning.module in app.module :
Nest can't resolve dependencies of the PlanningService (?). Please make sure that the argument PlanningMo
del at index [0] is available in the PlanningModule context.
Potential solutions:
- If PlanningModel is a provider, is it part of the current PlanningModule?
- If PlanningModel is exported from a separate #Module, is that module imported within PlanningModule?
#Module({
imports: [ /* the Module containing PlanningModel */ ]
})
this is my planning.module
import { Module } from '#nestjs/common';
import { PlanningController } from './planning.controller';
import { PlanningService } from './planning.service';
import {MongooseModule} from "#nestjs/mongoose";
import {PlanningSchema} from "./schemas/planning.schema";
import {UserModule} from "../user/user.module";
#Module({
imports: [MongooseModule.forFeature([{ name: 'Planing', schema: PlanningSchema }]),UserModule],
providers: [PlanningService],
exports: [PlanningService],
controllers: [PlanningController]
})
export class PlanningModule {}
app.module.ts
import {Module, NestModule, MiddlewareConsumer, RequestMethod} from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '#nestjs/mongoose';
import { mongoConfig } from './config';
import { UserModule } from './user/user.module';
import { AuthModule } from './auth/auth.module';
import { JwtMidelware } from './utils/jwtMidelware';
import {UserController} from "./user/user.controller";
import { TeamModule } from './team/team.module';
import { CompanyModule } from './company/company.module';
import { AgenceModule } from './agence/agence.module';
import { PlanningModule } from './planning/planning.module';
#Module({
imports: [
MongooseModule.forRoot(mongoConfig.url, mongoConfig.auth),
PlanningModule,
UserModule,
AuthModule,
TeamModule,
CompanyModule,
AgenceModule,
]
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(JwtMidelware)
.exclude(
{ path: 'users/id', method: RequestMethod.GET },
)
.forRoutes(UserController);
}
}
In your constructor you have #InjectModel('Planning') but in your MongooseModule.forFeature() you have { name:'Planing',...}. These strings need to match.

Nest can't resolve dependencies of the JobsService (?). Please make sure that the argument JobModel at index [0] is available in the AppModule context

I'm really confused, I got this weird error and I have no idea.
Error: Nest can't resolve dependencies of the JobsService (?). Please make sure that the argument JobModel at index [0] is available in the AppModule context.
app.module.ts
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '#nestjs/mongoose';
import { JobsModule } from './jobs/jobs.module';
import { JobsService } from './jobs/jobs.service';
import { JobsController } from './jobs/jobs.controller';
import config from './config/config';
#Module({
imports: [JobsModule, MongooseModule.forRoot(config.mongoURI, {
useFindAndModify: false,
})],
controllers: [AppController, JobsController],
providers: [AppService, JobsService],
})
export class AppModule {}
/jobs/jobs.module.ts
import { Module } from '#nestjs/common';
import { MongooseModule } from '#nestjs/mongoose';
import { JobsService } from './jobs.service';
import { JobsController } from './jobs.controller';
import { JobsSchema } from './schemas/jobs.schema';
#Module({
imports: [MongooseModule.forFeature([{ name: 'Job', schema: JobsSchema }])],
controllers: [JobsController],
providers: [JobsService],
})
export class JobsModule {}
/job/job.controller.ts
import { Controller, Get, Post, Put, Delete, Body, Param } from '#nestjs/common';
import { JobsService } from './jobs.service';
import { IJobs } from './interfaces/jobs.interface';
#Controller('jobs')
export class JobsController {
constructor(private readonly jobsService: JobsService){}
#Get()
findAll():Promise<IJobs[]> {
return this.jobsService.findAll();
}
}
/job/jobs.service.ts
import { Injectable } from '#nestjs/common';
import { InjectModel } from '#nestjs/mongoose';
import { Model } from 'mongoose';
import { IJobSchema } from './schemas/jobs.schema';
import { IJobs } from './interfaces/jobs.interface';
#Injectable()
export class JobsService {
constructor(#InjectModel('Job') private readonly JobsModel: Model<IJobSchema>){}
async findAll():Promise<IJobs[]> {
return await this.JobsModel.find();
}
}
you already declared JobsService in your JobModule, delete it from Appmodule providers
import { Module } from '#nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '#nestjs/mongoose';
import { JobsModule } from './jobs/jobs.module';
import { JobsService } from './jobs/jobs.service';
import { JobsController } from './jobs/jobs.controller';
import config from './config/config';
#Module({
imports: [JobsModule, MongooseModule.forRoot(config.mongoURI, {
useFindAndModify: false,
})],
controllers: [AppController, JobsController],
providers: [AppService], // Here..
})
export class AppModule {}

Nestjs repository pattern config challenge

I'm trying to figure out how the "Repository Pattern" of TypeOrm in Nest works. I would like to have a resources file in which a local and a remote file hold the different entites, controllers, modules etc. See screenshot
When the app is building I`m getting the following error:
[Nest] 3186 - 11/19/2018, 10:44:43 PM [ExceptionHandler] No repository for "Project" was found. Looks like this entity is not registered in current "default" connection? +1ms
From the Nest and TypeORM documentation I can triangulate, that I have to tell the application where it can find the entities or at least this is what I believe the error is trying to tell me.
I'm using a .env to pull the config for TypeORM in:
TYPEORM_CONNECTION = postgres
TYPEORM_HOST = localhost
TYPEORM_USERNAME = xyz
TYPEORM_PASSWORD = xyz
TYPEORM_DATABASE = xyz
TYPEORM_PORT = 5432
TYPEORM_SYNCHRONIZE = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = src/server/**/**.entity{.ts,.js}
app.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { AngularUniversalModule } from './modules/angular-universal/angular-universal.module';
import { JiraService } from './services/jira.service'
// modules
import { ProjectModule } from './resources/local/project/project.module'
// sync
import {ProjectsSync} from './sync/projects.sync'
#Module({
imports: [
ProjectModule,
TypeOrmModule.forRoot(),
AngularUniversalModule.forRoot(),
],
controllers: [],
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}
project.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { ProjectService } from './project.service';
import { ProjectController } from './project.controller';
import { Project } from './project.entity';
#Module({
imports: [TypeOrmModule.forFeature([Project])],
providers: [ProjectService],
controllers: [ProjectController],
})
export class ProjectModule {}
project.service.ts
import { Injectable, Inject } from '#nestjs/common';
import { InjectRepository } from '#nestjs/typeorm';
import { Repository } from 'typeorm';
import { Project } from './project.entity';
#Injectable()
export class ProjectService {
constructor(
#InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
) {}
async findAll(): Promise<Project[]> {
return await this.projectRepository.find();
}
}
project.entity.ts
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
#Entity()
export class Project {
#PrimaryGeneratedColumn()
id: number;
#Column({ length: 500 })
name: string;
}
project.controller.ts
import { Controller, Get } from '#nestjs/common';
import { ProjectService } from './project.service';
import { Project } from './project.entity';
#Controller('project')
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}
#Get()
findAll(): Promise<Project[]> {
return this.projectService.findAll();
}
}
Just a wild guess, have you tried:
TYPEORM_ENTITIES = ./server/**/**.entity{.ts,.js}
And/or switching the imports in app.module.ts putting the TypeOrmModule first:
#Module({
imports: [
TypeOrmModule.forRoot(),
ProjectModule,
AngularUniversalModule.forRoot(),
],
controllers: [],
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}
In your .env file change
TYPEORM_ENTITIES = src/server/**/**.entity{.ts,.js}
to
TYPEORM_ENTITIES = /../**/*.entity.{js,ts}
in your app.module use:
const typeOrmConfig: TypeOrmModuleOptions = {
type: process.env.TYPEORM_CONNECTION,
host: process.env.TYPEORM_HOST,
port: process.env.TYPEORM_PORT,
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE,
synchronize: process.env.TYPEORM_SYNCHRONIZE,
logging: process.env.TYPEORM_LOGGING,
entities: [__dirname + process.env.TYPEORM_ENTITIES]
}
#Module({
imports: [
TypeOrmModule.forRoot(typeOrmConfig),
ProjectModule,
AngularUniversalModule.forRoot(),
],
controllers: [],
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}

Resources