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.
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.
Share Improve this question edited Mar 29, 2017 at 18:31 NobleUplift asked Mar 27, 2017 at 20:44 NobleUpliftNobleUplift 6,0438 gold badges49 silver badges93 bronze badges2 Answers
Reset to default 4If you have monjs 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;
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");