Nest may struggle with resolving dependencies at times, but rest assured they are indeed present

I've encountered a strange issue. Nest is flagging a missing dependency in a service, but only when that service is Injected by multiple other services.

cleaning.module.ts

@Module({
imports: [
 //Just a few repos
],
providers: [
    ServicesService,
    ...
    AreasService,
    RoomsService,
    ...
],
controllers: [
...
],
exports: [
    ServicesService,
    ...
    AreasService,
    RoomsService,
    ...
]})

services.module.ts, areas.module.ts, and rooms.module.ts all have similar structures.

@Module({
  imports: [CleaningModule],
  providers: [],
  controllers: [],
  exports: []
})

rooms.service.ts

constructor(@InjectRepository(Room) private readonly repo: Repository<Room>, private areasService: AreasService) { }

services.service.ts

constructor(@InjectRepository(Service) private readonly repo: Repository<Service>, ... private roomsService: RoomsService) { }

This setup leads to the following error message:

ERROR [ExceptionHandler] Nest can't resolve dependencies of the RoomsService (RoomRepository, ?). Please make sure that the argument dependency at index [1] is available in the CleaningModule context.

It seems that AreasService is somehow missing, even though it should be provided by the CleaningModule.

The odd thing is, if I eliminate the injection of RoomsService in the constructor of ServicesService, everything works perfectly fine. The reasoning behind this behavior eludes me...

Answer №1

Upon encountering the error, it appears that there is a circular file import happening between your roo.service.ts and area.service.ts. This could involve multiple layers of files, for example, room.service.ts importing area.service.ts which then imports another file eventually leading back to room.service.ts, or it could be related to the use of barrel files. If indeed this is a circular dependency scenario where room uses area and vice versa, you will need to utilize

@Inject(forwardRef(() => OtherService))
. Otherwise, you must find a solution to avoid the circular import issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to remove all attributes from one interface when comparing to another?

Consider the following two interfaces: interface A { a: number; b: string; } interface B { b: string; } I am interested in creating a new type that includes all the keys from interface A, but excludes any keys that are also present in interface B. ...

Tips for efficiently deconstructing JSON arrays, objects, and nested arrays

I'm attempting to destructure a JSON file with the following structure: [ { "Bags": [ { "id": 1, "name": "Michael Kors Bag", "price": 235, "imgURL" ...

Join a subscription and remain subscribed in sequential order

Within the code below, there is a nested subscribe function. It takes a schedule_id and retrieves questions based on that schedule_id. The functionality works correctly, but the order in which getQuestion() is executed is not guaranteed. Schedule IDs: 111, ...

"Step-by-step guide: Accessing the upload folder in Node.js with Express

I successfully implemented an upload feature in my API using multer. However, I'm facing issues while trying to access the uploaded files from Gatsby. router.use("/public", express.static(__dirname + "/public")); The uploaded files are located in /pu ...

What are the drawbacks of calling async/await within a fresh Promise() constructor?

I have implemented the async.eachLimit function to manage the maximum number of operations concurrently. const { eachLimit } = require("async"); function myFunction() { return new Promise(async (resolve, reject) => { eachLimit((await getAsyncArray ...

Callback for dispatching a union type

I am currently in the process of developing a versatile function that will be used for creating callback actions. However, I am facing some uncertainty on how to handle union types in this particular scenario. The function is designed to take a type as inp ...

minimize the size of the image within a popup window

I encountered an issue with the react-popup component I am using. When I add an image to the popup, it stretches to full width and length. How can I resize the image? export default () => ( <Popup trigger={<Button className="button" ...

Allow for an optional second parameter in Typescript type definition

Here are two very similar types that I have: import { VariantProps } from "@stitches/core"; export type VariantOption< Component extends { [key: symbol | string]: any }, VariantName extends keyof VariantProps<Component> > = Extra ...

UI-Router is malfunctioning, causing ui-sref to fail in generating the URL

I'm currently working on a project that involves Angular, Express, and UI-router for managing routes. While I've properly configured my $states and included the necessary Angular and UI-router libraries in my HTML file, I am facing an issue wher ...

Exploring secure routes in Node.js with test cases using Mocha and Chai?

The function verifies whether the route is accessible or not function checkSessionCookieValidity(req, res, next) { if (!isValid(req.session)) { return res.status(401).json({ isLoggedIn: false }); } return next ...

Transform an array into an object with assigned key names

How can we transform the following array: [2019,2020,2021] into the format: { 0: {year:2019}, 1: {year:2020}, 2: {year:2021} } ...

When invoked, a Javascript Object comes back empty

My code snippet: const channels = fauna.paginate(q.Match(q.Index("channels"), "true")) // Query FaunaDB database for channel list => create constant called users containing results const channelList = channels.each(function (page) { ...

Can a React function component be typed with TypeScript without the need for arrow functions?

Here is my current React component typed in a specific way: import React, { FunctionComponent } from "react"; const HelloWorld : FunctionComponent = () => { return ( <div> Hello </div> ); } export default HelloWorld; I ...

(updated solution) The Express web app service is encountering an error message stating "SyntaxError: Unexpected token >"

I recently deployed my web app to two Azure app services and encountered different results - one is working fine, while the other is throwing an error. My web app is built on Express and I am unsure how to resolve this issue as the error does not seem to ...

Passport sessions persist even after the browser is closed

I'm currently working on implementing remember me functionality for my Node.js server using passport for session management. However, I've encountered an issue where the session cookie connect.sid is not being destroyed on browser close, which sh ...

Ways to display the outcomes of an http query using Express?

How can I utilize Request and Express to retrieve the result of my http request in order to display it? var request = require('request'); var http = require('http'); exports.index = function(req, res){ var apiUrl = 'http://api ...

Strategies for extracting the type argument from a nested property and transforming it into a different value

I’m struggling to find the right way to frame my question, so I’ll provide an example of what I need help with. Let's assume I have the following object: const obj = { one: 'some string', two: new Set<string>(), }; Now, I wan ...

Executing a Node.js program using the command line without having to type out the word 'node'

I am currently working on a Node.js package that includes a command-line utility. Right now, alacon.js is situated in the root of the package. To execute this utility, I have to use the word node followed by the utility's name, like so: node alacon ...

Unexpected outcome from querying an element within an array located in an object in a mongoose schema

Presently, I am facing an issue with a mongoose schema structure. It appears as follows: { "_id" : ObjectId("some_id"), "repDet" : { "devIDs" : [ "dev1_B37", "dev2_B38", "dev3_B26" ], ...

Upon successful authorization, the Node Express server will pass the access token to the React client app via OAuth in the callback

I am currently working on a node server that authenticates with a third party using oauth, similar to how Stack Overflow does. After authorizing the request and obtaining the access token and other essential information from the third party, my goal is to ...