美好的一天!
我创建了一个带有基本端点的基本控制器 BaseController ...
import { Get, Post, Put, Delete, HttpStatus, Request, Response } from '@nestjs/common';
import { MessageCodeError } from './../index';
export class BaseController {
public dataService: any;
constructor(public DataService: any) {
this.dataService = DataService;
}
@Get('/')
public async findAndCountAll(@Request() req, @Response() res) {
const params = req.query;
const offset = Number(params.skip) || 0;
const limit = Number(params.limit) || 10;
delete params.offset;
delete params.limit;
const records = await this.dataService.findAndCountAll({ where: params, offset, limit });
return res.status(HttpStatus.OK).send({ total: records.count, data: records.rows });
}
}
我尝试在其他控制器中使用它。例如,在 UserController ...
中import { Controller, Get, Post, Put, Delete, HttpStatus, Request, Response } from '@nestjs/common';
import { MessageCodeError } from '../common/index';
import { UserService } from './user.service';
import { BaseController } from './../common/shared/base.controller';
@Controller('users')
export class UserController extends BaseController {
constructor(public userService: UserService) {
super(userService);
}
}
我认为,因为我在课堂 UserController 从类 BaseController 继承,所以函数(充当端点)应该在 UserController中可用。但似乎我错了......
数据库没问题,连接稳定,代码正常工作(至少在我决定从 UserController 中取出端点到 BaseController 之前)。可能是什么错误?
答案 0 :(得分:0)
使用BaseController
装饰器注释您的@Controller()
。
import { Get, Post, Put, Delete, HttpStatus, Request, Response } from '@nestjs/common';
import { MessageCodeError } from './../index';
@Controller()
export class BaseController {
public dataService: any;
constructor(public DataService: any) {
this.dataService = DataService;
}
@Get('/')
public async findAndCountAll(@Request() req, @Response() res) {
const params = req.query;
const offset = Number(params.skip) || 0;
const limit = Number(params.limit) || 10;
delete params.offset;
delete params.limit;
const records = await this.dataService.findAndCountAll({ where: params, offset, limit });
return res.status(HttpStatus.OK).send({ total: records.count, data: records.rows });
}
}