比如我在 service 里定义了几个函数
getA() // 获取 A 的数据
getB() // 获取 B 的数据
router:
api/ab/a 可以获取 a 的数据
api/ab/b 可以获取 b 的数据
现在想让 api/ab 显示 a 和 b 所有的数据,
是不是我就应该写了一个 getAB() 然后用 getAB() 这个函数获取 a 和 b 所有数据呢?
这个格式怎么写啊?
我现在是这么写的,不过获取老是空。
router
router.get('/classifications', classificationController.index);
controller
/**
* classification 列表
*/
export const index = async (
request: Request,
response: Response,
next: NextFunction,
) => {
try {
const classification = await getClassification();
response.send(classification);
} catch (error) {
next(error);
}
};
service
import { connection } from '../app/database/mysql';
/**
* 获取 classification
*/
export const getClassification = async () => {
const dataArry = {
category: null,
grade: null,
version: null,
subject: null,
}
dataArry.category = getCategory();
dataArry.grade = getGrade();
dataArry.version = getVersion();
dataArry.subject = getSubject();
return dataArry;
};
/**
* 获取 category 列表
*/
export const getCategory = async () => {
const statement = `
SELECT
DISTINCT(category)
FROM post
`;
const [data] = await connection.promise().query(statement);
return data;
};
/**
* 获取 grade 列表
*/
export const getGrade = async () => {
const statement =
`
SELECT
DISTINCT(grade)
FROM post
ORDER BY FIELD(SUBSTRING(grade,1,1),'一','二','三','四','五','六','七','八','九');
`;
const [data] = await connection.promise().query(statement);
return data;
};
/**
* 获取 version 列表
*/
export const getVersion = async () => {
const statement = `
SELECT
DISTINCT(version)
FROM post
`;
const [data] = await connection.promise().query(statement);
return data;
};
/**
* 获取 subject 列表
*/
export const getSubject = async () => {
const statement = `
SELECT
DISTINCT(subject)
FROM post
`;
const [data] = await connection.promise().query(statement);
return data;
};
不用 getClassification ()这个方法,直接这样就解决了