0

this is not available in the methods. More details in my code (comment blocks) about what is available, it's difficult to explain this in plain text. This is probably a duplicate but I don't find a straight answer anywhere.

Is something wrong with this code, or is there a logic workaround?

Class

class CheckParameters {  
    get parameters() {
        return this._parameters;
    }                                                                                                     
    constructor(parameters) {                                                                                                 
        this._parameters = parameters;                                                                                        
        console.log(this);   //this is defined                                                                             
    }                                                                                                                         
                                                                                                                              
    CheckQueryParam (req, res, next) {  
        console.log(this);   // undefined                                                                                      
        let parameterChecks = this._parameters.map(param => { // TypeError: Cannot read property '_parameters' of undefined   
            return new Promise((resolve, reject) => {...});                                                                   
        });                                                                                                                   
        Promise.all(parameterChecks)                                                                                          
            .then(() => next())                                                                                               
            .catch((error) => {                                                                                               
                console.error(error);                                                                                         
                res.status(400).send(error)                                                                                   
            });                                                                                                               
    }                                                                                                                         
}

Using the class

console.log(LimitAndOffset.parameters); //Also this is defined

const LimitAndOffset = new CheckParameters([ 
    new Parameter(    //Parameter is another class, this works perfectly                       
        "limit",                             
        Number,                              
        [Validators.Number],                 
        true                                 
    ),                                       
    new Parameter(                           
        "offset",                            
        Number,                              
        [Validators.Number],                 
        true                                 
    )                                        
]);  

app.get('/', LimitAndOffset.CheckQueryParam, function (req, res) {
    res.status(200).json('piemel');
});

                                    
                                                                                                                     
Community
  • 1
  • 1
Joey Dossche
  • 141
  • 1
  • 8

0 Answers0