Reputation: 1
I am creating an CRUD API using mongodb. I got an error of to add employeeModel at index[0] but I don't create any employeeModel in my code. I just created an interface and schema. Here is the link of my code. Please provide me a solution.
The error I've got:
Nest can't resolve dependencies of the CrudService (?). Please make sure that the argument employeeModel at index [0] is available in the AppModule context.
code link: https://github.com/salil20495/salilchoudhary41-gmail.com/tree/master/src
Upvotes: 0
Views: 245
Reputation: 13644
I have cloned your repo and went through it.
Issue which looks causing the issue is the issue I've posted in the commend.
As it is:
constructor(@InjectModel('employee') private readonly jobmodel:Model<employeeModel>){}
Should be:
import {model, Model} from 'mongoose'
import {InjectModel} from '@nestjs/mongoose'
@Injectable()
export class CrudService {
constructor(@InjectModel('employee') private readonly Model<employeeModel>){}
As the side note: please try to use Prettier - your code is really hard to read.
I see many other TypeScript error there, but the dependency issue is solved. If you find other issues, please push your changes to master and let me know in the comment
UPDATE:
I have fixed your code. PR is submitted here: https://github.com/salil20495/salilchoudhary41-gmail.com/pull/1
There plenty of stuff:
1. Mongoose schema - you were using badly created schema, that was why Nest couldn't provide the jobmodel
to the CrudService
as Mongoose was throwing an error.
2. DI scope. Dependencies are available within a module where they are defined as providers. You have to keep in mind. I have fixed it, but please review the changes to have better understanding.
3. Formatting - please set up a watcher or action on save to apply Prettier - code is much more often read then changed - it has to be readable!
I think the question is answered and answer can be accepted and closed. I hope it helps!
Upvotes: 1