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

Preventing Firebase duplicates leads to the error of not being able to read the property 'apps'

Struggling to incorporate Firebase into a TypeScript/NextJS project, I have encountered difficulties. Despite successfully importing and initializing the app: import * as firebase from "firebase/app"; import { collection, getDocs } from "fir ...

Announce enhancements to a Typescript library

Utilizing Sequency's extendSequence() feature to enhance all Sequence instances with a custom method: import Sequence, { extendSequence, isSequence } from 'sequency' import equal from '@wry/equality' class SequencyExtensions { e ...

How can you establish the default value for a form from an Observable?

Check out my TypeScript component below export interface Product{ id?:string, name:string, price:string; quantity:string; tags:Tags[]; description:string; files: File[]; } product$:Observable<Product | undefined>; ngOnIn ...

The functionality of TypeScript's .entries() method is not available for the type 'DOMTokenList'

I'm currently working with a stack that includes Vue3, Vite, and TypeScript. I've encountered an issue related to DOMTokenList where I'm trying to utilize the .entries() method but TypeScript throws an error saying Property 'entries&apo ...

Call a function within a stateless component in a React application

I have a question regarding my React component. I am attempting to call the function ButtonAppBar within my stateless component, but the TypeScript compiler is throwing an error stating '{' expected. I'm unsure whether I need to pass it to m ...

Mastering Typescript lookup types - effectively limit the properties included in a merge operation with the Partial type

Exploring lookup types, I'm interested in creating a safe-merge utility function that can update an entity of type T with a subset of keys from another object. The objective is to leverage the TypeScript compiler to catch any misspelled properties or ...

Using Typescript/JSX to assign a class instance by reference

Looking to access an object's property by reference? See the code snippet below; class Point{ x:number; y:number; constructor(x,y) { this.x=x; this.y=y; } } const a = { first: new Point(8,9), second: new Point(10,12) }; let someBoo ...

Introducing the concept of type-specific name inclusion

I am currently developing my Angular app using TypeScript with the goal of preventing redundancy through some form of generic handling. Here is where I am starting: class BaseProvider { api_url = 'http://localhost:80/api/FILL_OUT_PATH/:id&apo ...

How can you make an IonPopover dynamically appear from a button with the perfect positioning?

I want to display a popover below a button when the button is clicked, similar to the example on the Ion docs page. However, I am having trouble implementing this in React as the code provided is only for Angular. Here is my current code: ... <IonButt ...

Blurry text issue observed on certain mobile devices with Next.js components

There continues to be an issue on my NextJS page where some text appears blurry on certain mobile devices, particularly iPhones. This problem is only present on two specific components - both of which are interactive cards that can be flipped to reveal the ...

Should private members be kept confidential during program execution?

While Typescript's "private" members may not be truly private at runtime, traditional closures maintain the privacy of their members. Is there value in ensuring that private members remain private during runtime? ...

Using getters in a template can activate the Angular change detection cycle

When using getters inside templates, it seems that Angular's change detection can get stuck in a loop with the getter being called multiple times. Despite researching similar issues, I have not been able to find a clear solution. Background info: I ...

Using Required and Partial with an Array of Generic Types

I'm currently working with the following types: interface Color { color: string } type DarkerColor<T> = T & Color & { darker: string } type ColorInfo<T> = DarkerColor<T> & { hue: number luminance: number opacity ...

Using 'interface' declarations from TypeScript is unsupported in JS for React Native testing purposes

I have a ReactNative app and I'm attempting to create a test using Jest. The test requires classes from a native component (react-native-nfc-manager), and one of the needed classes is defined as follows export interface TagEvent { ndefMessage: N ...

Creating a return type in TypeScript for a React Higher Order Component that is compatible with a

Currently utilizing React Native paired with TypeScript. Developed a HOC that functions as a decorator to add a badge to components: import React, { Component, ComponentClass, ReactNode } from "react"; import { Badge, BadgeProps } from "../Badge"; functi ...

Definition in Typescript: The term "value is" refers to a function that takes in any number of arguments of

export function isFunction(value: any): value is (...args: any[]) => any { return typeof value === 'function'; } What is the reason behind using value is (...args: any[]) => any instead of boolean ? ...

Connect an Observable to the template with async binding

I've been attempting to link an Observable to a template variable in Angular using the following code: [class.active]="isBookmarked$ | async" During the ngOnInit function, I initialize the Observable: var promise = this.cacheService.getItem(this.bo ...

The upcoming construction of 'pages/404' page will not permit the use of getInitialProps or getServerSideProps, however, these methods are not already implemented in my code

Despite my efforts to search for a solution, I have not found anyone facing the same issue as me. When I execute next build, an error occurs stating that I cannot use getInitalProps/getServerSideProps, even though these methods are not used in my 404.tsx f ...

Transforming TypeScript declaration files into Kotlin syntax

Has there been any progress on converting d.ts files to Kotlin? I came across a post mentioning that Kotlin developers were working on a converter, but I am unsure about the current status. I also found this project, which seems to be using an outdated c ...

Switch back and forth between two pages within the same tab on an Ionic 3 app depending on the user's login information

I am seeking a way to switch between two profile pages using the profile tab based on whether the user is a student or tutor in an ionic 3 application. ...