The proper injection of the NestJS module is essential for seamless functionality

When working with Nest, I created a new module using the command nest g module UserModule

src/user-module/user.resolver.ts contains the following code:

import { Query, Resolver } from '@nestjs/graphql';
import { UserService } from './user.service';

export class UserResolver {
  
  constructor (private readonly user: UserService) {
   this.user = user; 
  }

  @Query('users')
  async users() {
    return this.user.getUsers();
  }
}

and then

src/user-module/user-module.module.ts
looks like this -

import { Module } from '@nestjs/common';
import { UserResolver } from './user.resolver';
import { UserService } from './user.service';
import { PrismaService } from './prisma.services';

@Module({
  providers: [UserResolver, UserService, PrismaService]
})

export class UserModuleModule {}

The file src/user-module/user.service.ts defines the UserService:

import { Injectable } from "@nestjs/common";
import { PrismaService } from "./prisma.services";
import { User } from "src/graphql";

@Injectable()
export class UserService {
  constructor(private readonly prisma: PrismaService) {

  }

  async getUsers(): Promise<User[]> {
    return this.prisma.prismaUsers.findMany({})
  }
}

The

src/user-module/prisma.services.ts
includes the PrismaService:

import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    await this.$connect();
  }
}

Starting the service with npm start, I ran a query and encountered an error:

query {
   users {
     id
     name
     email
   }
}

The error message received is as follows:

[Nest] 24682 - 09/09/2023, 10:59:34 AM ERROR [ExceptionsHandler] Cannot read properties of undefined (reading'getUsers') TypeError: Cannot read properties of undefined (reading 'getUsers') ...

After reviewing my code on GitHub, I am still unsure why my injection is not functioning correctly. This differs from the issue discussed in this Stack Overflow post.

Answer №1

Ensure that your UserResolver class includes the @Resolver() decorator to signal Nest framework that it is a resolver and to instruct Typescript to include type metadata in the constructor of the class. This step is similar to adding @Injectable() to a standard provider, as it prompts Typescript to generate the necessary metadata.

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

the "then" function is causing issues in my TypeScript Node code

My code looks like this: var fs = require('fs'); var util = require('util'); var files = fs.readdirSync('*/path to my folder that contains subfolders*/') async function getfilenum(){ var files_v_num = [] for(const i in fi ...

Challenges associated with implementing HTTP in Ionic version 3

I've been working on developing an app using Ionic 3 and I decided to implement the HTTP module. For reference, I relied on the official documentation provided by the Ionic framework. Documentation Link: https://ionicframework.com/docs/native/http/ ...

What data type should be used for process.stdin in a TypeScript/Node.js environment?

When faced with a variable that can either be fs.createReadStream('file-path') or process.stdin, the decision on which type to use can be difficult. It is important to determine if both returns are of type net.Socket, fs.ReadStream, or stream.Red ...

Unable to refresh the context following a successful API call

My current project in NextJS requires a simple login function, and I have been attempting to implement it using the Context API to store user data. However, I am facing an issue where the context is not updating properly after fetching data from the back-e ...

Error in Angular8: Attempting to loop through an undefined property

I have searched tirelessly for a solution, but none of them seem to work. Every time I click on the edit button, it redirects me to edit-event page and shows me this error: ERROR TypeError: Cannot read property 'categories' of undefined n ...

Having trouble making optional parameters overload in Typescript?

Let's consider a simplified instance (although overloading may not be necessary in this case due to the simplification) UPDATE: The initial example provided did not fully illustrate the issue, so here is an improved version: function fn <T>( / ...

What is the best way to store values in a map for future reference within a Kotlin class?

Looking to implement a map of key value pairs in Kotlin inside a class that is mutable and can be updated and referenced as needed. Research suggests that using a MutableMap would be the appropriate choice, given its ability to be updated at any point. I ...

Tips for retrieving modified data from a smart table in Angular 4

Currently, I am working on an angular project where I am utilizing smart table. Here is a snippet of my .html file: <ng2-smart-table [settings]="settings" [source]="source" (editConfirm)="onSaveConfirm($event)" (deleteConfirm)="onDeleteConfirm($event ...

Exploration of mapping in Angular using the HttpClient's post

After much consideration, I decided to update some outdated Angular Http code to use HttpClient. The app used to rely on Promise-based code, which has now been mostly removed. Here's a snippet of my old Promise function: public getUser(profileId: nu ...

Module error caused by Typescript path inconsistency

After creating a new model named "project" within the existing project, I encountered an error when attempting to import the class into another typescript file in VS2019. The specific error message thrown is as follows: "ts2307 cannot find module ' ...

Troubleshooting: Socket.io integration in Angular is not functioning within a .then() statement

Upon running this code snippet in a component: const videoholder = <HTMLDivElement>( document.querySelector('#videoholder') ); const myPeer = new Peer(this.userid, { host: '/', ...

When using Angular version 13 alongside rxjs 7.4 and TypeScript 4+, an issue arises with the error message: "Declaration file for module 'rxjs' not found"

Currently embarking on a new Angular app using V13 and rxjs7.4, I encountered the error shown above when trying to import: import { BehaviorSubject } from 'rxjs'; Initially, I attempted to address this by creating a typings.d.ts declaration as s ...

Eliminate the need to input the complete URL every time when making server calls

Currently, my springbok application has a React-Typescript frontend that is functioning well. I am using the request-promise library to make requests like this: get('http://localhost:8080/api/items/allItems', {json: true}). However, I would like ...

GraphQL-HTTP Query Not Found

After diligently following the steps to integrate GraphQL from the provided documentation for ExpressJS, which can be found at https://github.com/graphql/graphql-http#with-express import express from 'express'; // yarn add express import { create ...

Troubleshooting a GET Request Hanging Issue with Next.js 13 Route Handler

I'm currently encountering an issue with the new routing feature in my Next.js 13 project. I have a route handler set up in app/api/ingresos/route.ts with the code snippet below: import { NextResponse } from 'next/server'; import PocketBase ...

Leveraging the power of Angular 5 to seamlessly integrate two distinct components on

I am exploring a way to render an additional component on the current page instead of navigating to a new one. Here is my setup: <button mat-button color="primary" [routerLink]="['/tripspath', trip.tripId]" style="cursor: pointer">View Rou ...

Determine the time difference between the beginning and ending times using TypeScript

Is there a way to calculate the difference between the start time and end time using the date pipe in Angular? this.startTime=this.datePipe.transform(new Date(), 'hh:mm'); this.endTime=this.datePipe.transform(new Date(), 'hh:mm'); The ...

Personalized style for text overflow property

The application is created using Angular. Within a component, we have a div containing some text: <div>abcdefghijklmnop<div> Depending on the screen size, the text should either be fully displayed or clipped. I discovered the property 'te ...

Turn off the interconnected route while utilizing npm to display the tree of dependencies

If I want to display the project's dependencies tree using npm, I would use the following command: npm ls lodash The output will look something like this: > npm ls lodash npm info using [email protected] npm info using [email protected] ...

Is there a way to modify the button exclusively within the div where it was pressed?

I plan to incorporate three buttons in my project (Download, Edit, and Upload). Upon clicking the Download button, a function is triggered, changing the button to Edit. Clicking the Edit button will then change it to Upload, and finally, clicking the Uplo ...