sql - TypeORM:如何从响应中排除关键属性?
问题描述
我有以下查询:
cors(request, response, async () => {
const connection = await connect();
const allAvailable = await connection
.createQueryBuilder(Topics, 'topics')
.select('topics.UID')
.where('topics.tickets = 0')
.andWhere('topics.chips = 0')
.andWhere('topics.masteredLevel <= 1')
.getRawMany();
response.send(allAvailable);
})
我的回答是:
[{"topics_UID":"T001"},{"topics_UID":"T002"}]
但我想要这个回应:
["T001", "T002"]
解决方案
您可以使用将其映射到该属性在查询后添加
allAvailable = allAvailable.map(t => t.topics_UID);
const allAvailable = (await connection
.createQueryBuilder(Topics, 'topics')
.select('topics.UID')
.where('topics.tickets = 0')
.andWhere('topics.chips = 0')
.andWhere('topics.masteredLevel <= 1')
.getRawMany()).map(t => t.topics_UID);