3

I have a Node.js server built with Express.js and coded in TypeScript. Here's a snippet of the get call for my server:

server.ts

private get(req: Request, res: Response, next: NextFunction, objectName: string) {
    var DatabaseObject = require("./models/" + objectName + ".js")(this.orm, Sequelize.DataTypes);
    var Transform = require("./routes/" + objectName + ".js");
    var transform = new Transform();

    // ...

    console.log(req.query["columns"]);
    console.log(transform.columnWhitelist);
    console.log(transform);

    // ...

    if (transform.columnWhitelist) {
        console.log("Column Whitelist Exists.");
    }
    // ...
}

It dynamically loads the Sequelize module for the database object being requested in the URL and then tries to load a TypeScript module with rules on what columns can be selected, which columns can be queried on, etc. Here's the beginning of my ruleset class:

account.ts

export default class Transform {
    static columnWhitelist : Object = {"id": "id", "name": "name", "parentAccountId":"parentAccountId", "masterAccountId":"masterAccountId"};

    constructor() { }
}

However, running my application, I get:

id,name,parentAccountId
undefined
{ default: 
   { [Function: Transform]
     columnWhitelist: 
      { id: 'id',
        name: 'name',
        parentAccountId: 'parentAccountId',
        masterAccountId: 'masterAccountId' } } }

Making the call transform.columnWhitelist, I get undefined, despite seeing it in the generated JavaScript file as well. I've also tried just:

    var transform = require("./routes/" + objectName + ".js");

Or:

    var transform = require("./routes/" + objectName + ".js")();

But neither of these work either.

2 Answers 2

4

If you have commonjs as a module in your tsconfig.json and Transform class exported as default you should probably import it as

 var transform = require("./routes/" + objectName + ".js").default;
Sign up to request clarification or add additional context in comments.

1 Comment

I get it now. Thanks a lot! At one point I did try var Transform = require("./routes/" + objectName + ".js"); var transform = Transform.Transform; or something like that but I must've typed something wrong.
0

If you do not want to use

var transform = require("./routes/" + objectName + ".js").default;

You can export your class by doing

class Transform {
     static columnWhitelist: Object = {"id": "id", "name": "name", "parentAccountId": "parentAccountId", "masterAccountId": "masterAccountId"};

     constructor () {}
}

export = Transform;

After you can do again as before:

var transform = require ("./ routes /" + objectName + ".js");

2 Comments

Is export = Transform; TypeScript-specific syntax versus the module.exports = ... of JavaScript, or another way of writing the latter?
export = is used to give typescript the export default behavior. In addition it works with interfaces, functions, enums etc ... You can find out more by reading the documentation https://www.typescriptlang.org/docs/handbook/modules.html (you can do a search with export =) if you want to keep typing, you can add: var transform: typeof Transform = require("./routes/" +objectName+ ".js");

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.