Node.js typescript中的作用域
我在nodeJs中创建了一个示例应用程序,它使用pgpromise在Postgres中执行查询。并用一个名为PostgresDataAccess的类包装它。在下面的代码中,为什么我不能从get和getOne函数访问“dal”对象。“this”关键字返回undefined。有人能解释一下吗Node.js typescript中的作用域,node.js,typescript,scope,Node.js,Typescript,Scope,我在nodeJs中创建了一个示例应用程序,它使用pgpromise在Postgres中执行查询。并用一个名为PostgresDataAccess的类包装它。在下面的代码中,为什么我不能从get和getOne函数访问“dal”对象。“this”关键字返回undefined。有人能解释一下吗 import { Router, Request, Response, NextFunction } from 'express'; import { PostgresDataAccess } from '..
import { Router, Request, Response, NextFunction } from 'express';
import { PostgresDataAccess } from '../dal/postgres';
const config = require('../../config/config');
export class ProjectRouter {
public router: Router;
dal: PostgresDataAccess;
/**
* Construct the Project Router.
*/
constructor() {
this.dal = new PostgresDataAccess(config);
this.router = Router();
this.init();
}
/**
* Take each handler, and attach to one of the Express.Router's endpoints.
*/
init() {
this.router.get('/', this.get);
this.router.get('/:id', this.getOne);
}
/**
* GET: Returns all projects.
*/
get(req: Request, res: Response, next: NextFunction) {
let projects = this.dal.queryDb('select * from projects', null, res);
}
/**
* GET: Retuns one project by id.
*/
getOne(req: Request, res: Response, next: NextFunction) {
let projects = this.dal.queryDb('select * from projects where id = $1', [req.params.id], res);
}
}
const projectRouter = new ProjectRouter();
export default projectRouter.router;
为什么我不能从get和getOne函数访问“dal”对象。“this”关键字返回未定义的值
更改:
get(req: Request, res: Response, next: NextFunction) {
let projects = this.dal.queryDb('select * from projects', null, res);
}
到
作为
与getOne相同
为什么我不能从get和getOne函数访问“dal”对象。“this”关键字返回未定义的值
更改:
get(req: Request, res: Response, next: NextFunction) {
let projects = this.dal.queryDb('select * from projects', null, res);
}
到
作为
与getOne相同