0

I am getting an error "Cannot read property 'Items' of undefined" in javascript. this keyword is always undefined in the Base class.

how to resolve this issue? Is there any issue with static method to use in express router?

Typescript compile option is pointing to ES6.

import * as express from 'express';
import {Router, Request, Response, NextFunction, Wrap } from '../../../Core';
import {FacadeRequest} from '../../../Common';

class Base {
    public Items: any = { Name: 'Natarajan Ganapathi' };
    public Get(): any {
        return this.Items;
    }
}

class User extends Base {
    public GetUsers(): any {
        return super.Get();
    }
}

export type BoRegistry = { [key: string]: () => any };

export class Facade {
    public static BoRegistry: BoRegistry = {};
    public static async GetData(req: Request, res: Response, next: NextFunction): Promise<any> {
        let promises: Array<any> = [];
        let reqs = req.body as Array<FacadeRequest>;
        reqs.forEach(fReq => {
            let route = Facade.ParseRoute(fReq.Route);
            let func = Facade.BoRegistry[route.BoName];
            if (!func) {
                throw new Error(route.BoName + ' not register in BoRegistry');
            }
            let bo = func();
            promises.push(Facade.Invoke(bo, route, fReq));
        });
        return await Promise.all(promises);
    }

    private static async Invoke(bo: any, route: { BoName: string, MethodName: string }, fReq: FacadeRequest): Promise<any> {
        let api = bo[route.MethodName];
        if (!api) {
            throw new Error(route.MethodName + ' is not available in ' + route.BoName);
        }
        let res = await api(fReq.Request);
        return { [fReq.Key]: res };
    }

    private static ParseRoute(route: string): { BoName: string, MethodName: string } {
        let r = route.split('/');
        if (r.length < 2 || r.length > 2) {
            throw new Error('Request contains improper route');
        }
        return { BoName: r[0], MethodName: r[1] };
    }
}

Facade.BoRegistry['User'] = () => new User();

let router: Router = express.Router();
router.post('/' + Facade.GetData.name, Wrap(Facade.GetData));
export default router;
{
"Message": "Cannot read property 'Items' of undefined",
"Stack": "TypeError: Cannot read property 'Items' of undefined\n    
at Get (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:17:20)\n    
at GetUsers (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:22:22)\n    
at Function.<anonymous> (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:48:29)\n    
at next (native)\n    at __awaiter (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:7:65)\n    
at __awaiter (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:3:12)\n    
at Function.Invoke (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:43:16)\n    
at Base.static.GetData.reqs.forEach.fReq (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:37:38)\n    
at Array.forEach (native)\n    
at /Users/dist/dev/Server/Modules/Base/Router/Facade.js:30:18\n    
at next (native)\n    at __awaiter (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:7:65)\n    
at __awaiter (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:3:12)\n    
at GetData (/Users/dist/dev/Server/Modules/Base/Router/Facade.js:27:16)\n    
at exports.Wrap (/Users/dist/dev/Server/Core/Core.js:70:16)\n    
at Layer.handle [as handle_request] (/Users/node_modules/router/lib/layer.js:93:5)"
 }
3
  • 2
    Can you show where you're actually calling GetUsers? I see you instantiating a User, but never using this method. Commented Jul 2, 2016 at 0:26
  • 1
    Adding to what ndugger said, the value of this in Javascript depends on how you call your methods so its important to show the call sites too. Commented Jul 2, 2016 at 0:42
  • I am calling GetUsers from Invoke method. line let res = await api(fReq.Request); Commented Jul 2, 2016 at 3:24

1 Answer 1

0

"Cannot read property 'Items' of undefined",

Common JavaScript error. You need to be cautions of your usage of this or just use an arrow function.

More

https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.