Obtaining data from a TypeScript decorator

export class UploadGreetingController {
constructor(
private greetingFacade: GreetingFacade,

) {}
@UseInterceptors(FileInterceptor('file', {
    storage: diskStorage({
        destination: (req: any, file, cb) => {
            if (process.env.DEVELOPMENT === 'prod') {
                return cb(null, `${process.env.DEV_PATH_TO_GREETING_AUDIO}`);
            }
            return cb(null, `${process.env.PROD_PATH_TO_GREETING_AUDIO}`);
        },
        filename: async (req: any, file, cb) => {
            let uuid = await v4();
            req.params.uuid = uuid;
            return cb(null, `${uuid}.mp3`);
        }
    }),
    fileFilter: async (req, file: any, cb) => {
        let {userId, accountId} = req.user;
        let {greetingID} = req.params;
        if (greetingID === 'null' || !greetingID) {
            req.params.error = 'greeting:youShouldPassGreetingID';
            return cb(null, false);
        }
//In this section, I need to access the value of this.greetingFacade. How can I achieve that?
            let greetingEntity: any = await this.greetingFacade.getGreetingByUserIdAndAccountIdAndGreetingID(userId, accountId, greetingID);
            let type = (req.params.type) ? req.params.type : greetingEntity.type;
            if (type) {
                let type = (typeof req.params.type === 'string') ? parseInt(req.params.type) : req.params.type;
                if (type === 2) {
                    if (!file.originalname.match(/\.(mp3)$/)) {
                        return cb(null, false);
                    }
                    req.params.error = 'greeting:youShouldPassCorrectAudioFormat';
                    let deleteGreeting = (greetingEntity.uuid) ?
                        await this.greetingFacade.deleteAudioFromDisk(`${process.env.DEV_PATH_TO_GREETING_AUDIO}`, greetingEntity.uuid) : null;
                    return cb(null, file);
                }
                return cb(null, false);
            }
            return cb(null, false);
        }
    }))
}

The code above showcases my dilemma. I am trying to access the value of this.greetingFacade. However, it is currently inaccessible within the @UseInterceptors decorator. How can I make it accessible? It seems like I may need to modify or remove the

private greetingFacade: GreetingFacade
part of the code. Take a look at this specific code snippet for more context.

  //HERE I WANT TO GET ACCESS TO this.greetingFacade .How can I make it ?

Answer №1

To store an instance of a class or controller, simply define a property within your class and assign the instance to that property in the constructor method.

  private messageHandler:MessageHandler;

    export class NotificationController {
    constructor(messageHandler: MessageHandler) {

    this.messageHandler = messageHandler;

    }

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 Facebook provider is missing in Ionic Native

An error has occurred: No provider for Facebook!     InjectionError (core.es5.js:1231)     NoProviderError (core.es5.js:1269)     ReflectiveInjector_ ...

Enhance the efficiency of writing the *.d.ts file

I have recently started diving into TypeScript with React and encountered a new concept called a .d.ts file. I am wondering if there is an established best practice for writing this file or if it's more of a developer preference. Can someone verify if ...

The data in the Angular variable is not persisting

After calling this function to retrieve an array of Articles, I noticed that the data is not being saved as expected. Take a look at my console output below. GetAll() { //return this.http.get<Array<Article>>(this.cfg.SERVER); this.http.get ...

Troubleshooting and setting breakpoints in TypeScript code for Angular Web applications using the Firefox browser

Is there a method to add breakpoints to .typescript source files in my Angular application with Firefox Developer Tools? While I am able to add breakpoints to the generated javascript files, is it possible to debug the .ts source files directly? This quer ...

Is it possible to use non-numeric values as keys in a Typescript Map?

During a lesson: private items: Map<number, string> = new Map(); ... this.items[aNumber] = "hello"; Results in this error message: An element has an any type because the expression of type number cannot be used to index type Map<numbe ...

Bar chart in Highcharts vanishing following the update from version 10.2.1 to 10.3.1

I've been in the process of updating my highcharts to the latest version, but I've hit a roadblock. Specifically, I have a bar chart set up with the following configuration: { chart: { type: 'bar', ...

Collaborating on data through module federation

Currently, I am in the process of developing a Vue.js and TypeScript application using Vite. In addition, I am utilizing the vite-module-federation-plugin for sharing code across different applications. My main inquiry revolves around whether it is possibl ...

Issues with rapid refreshing are arising in Vite while dynamically importing components with React and typescript

In my quest to develop a multistep form, I have organized my data in a separate file for constants as shown below: import { lazy } from 'react'; export const steps = [ { id: 0, name: 'Personal Info', component: lazy(() ...

What is the best way to access a component's data within its method using Vue and Typescript?

Starting a Vue.js project with TypeScript and using single file components instead of class-styled ones has been my goal. However, I have encountered a recurring issue where I get error TS2339 when trying to reference my components' data using the "th ...

Determining Cost Using Quantity and Option Changes in Angular 4

Task In the shopping cart, there is a list of items with options, quantities, and prices. The goal is to calculate the total price based on any changes in the quantity and option of an item. Investigation I included [(ngModel)] for 2-way data binding, ...

Handling null values in React with Typescript and GraphQL: best practices

Dealing with nullable fields in GraphQL queries can be tricky. When working with a large nested query, handling all the null values cleanly becomes a challenge... For example, consider the following GraphQL query: query { markdown { authors { ...

How to Hide Parent Items in Angular 2 Using *ngFor

I am dealing with a data structure where each parent has multiple child items. I am trying to hide duplicate parent items, but accidentally ended up hiding all duplicated records instead. I followed a tutorial, but now I need help fixing this issue. I only ...

Interface key error caused by the TypeScript template literal

Version 4.4.3 of Typescript Demo Playground Example -- interface IDocument { [added_: `added_${string}`]: number[] | undefined; } const id = 'id'; const document: IDocument = { [`added_${id}`]: [1970] } My Attempts: I made sure that id in ...

How to specify the return type of a promise from an observer in Angular 6

Typically, I prefer using observables. However, in order to avoid 'callback hell' in this particular scenario, I decided to use toPromise(). Unfortunately, I encountered a lint error message when trying to define the return type: The 'Obj ...

What can be done to prevent the angular material select from overflowing the screen?

I have integrated an Angular Material Select component into my application, which can be found here: https://material.angular.io/components/select/overview. The issue I am facing is that when the select element is positioned near the bottom of the screen a ...

Activating functions based on radio button selection in React with TypeScript

Below are the radio buttons with their respective functions: <div className="row"> <div className="col-md-4"> <label className="radio"> <input onChange={() => {serviceCalc()}} ty ...

Beginner's Guide: Building your debut JavaScript/TypeScript library on GitHub and npm

I am looking to develop a simple JavaScript/TypeScript library focused on color conversion. Some of the functions and types I aim to export include: export type HEX = string; export type RGB = { r: number; g: number; b: number }; export type RGBA = { r: n ...

Can we incorporate 'this' in the super() constructor?

While reviewing someone else's code, I came across the following snippet: class foo extends bar { constructor() { super(param1, param2, new certainObject(this, otherParams)); } } The issue with this code is that it generates an error ...

Retrieving selected values from an ngx dropdown list

I am having trouble implementing ngx dropdown list in this way: <ngx-dropdown-list [items]="categoryItems" id="categoriesofdata" [multiSelection]="true" [placeHolder]="'Select categories'"></ngx-dropdown-list> ...

Alert: User is currently engaging in typing activity, utilizing a streamlined RXJS approach

In my current project, I am in the process of adding a feature that shows when a user is typing in a multi-user chat room. Despite my limited understanding of RXJS, I managed to come up with the code snippet below which satisfies the basic requirements for ...