if (filterLabel) {
result = result.filter((r) =>
(r.labels).map((l) => l.name === filterLabel));}
错误是";类型"Promise<"上不存在属性"map";标签[]>'&";。
如何筛选此数组?filterlabel是类似于"过滤器标签"的标签的名称;测试1";
export class Task {
// eslint-disable-next-line new-cap
@PrimaryGeneratedColumn()
id: number;
// eslint-disable-next-line new-cap
@Column()
name: string;
// eslint-disable-next-line new-cap
@Column()
description: string;
// eslint-disable-next-line new-cap
@CreateDateColumn()
created: string;
// eslint-disable-next-line new-cap
@UpdateDateColumn()
updated: string;
// eslint-disable-next-line new-cap
@ManyToMany(() => Label, (label) => label.tasks)
// eslint-disable-next-line new-cap
@JoinTable()
labels: Promise<Label[]>;
// eslint-disable-next-line new-cap
@OneToMany(() => Tracking, (tracking) => tracking.task, {nullable: true})
trackings: Promise<Tracking[]>;
}
export const getAllTasks = async (req, res) => {
const taskRepository = getRepository(Task);
const tasks = await taskRepository.find(
{relations: ['trackings', 'labels']});
const {filterTask, filterDescription, filterLabel} = req.query;
console.log('1', filterTask, '2', filterDescription, '3', filterLabel,
'4');
let result = [...tasks];
if (filterTask) {
result = result.filter((r) => r.name === filterTask);
}
if (filterLabel) {
filter();
}
if (filterDescription) {
result = result.filter((d) => d.description === filterDescription);
}
res.status(200).send({data: result});
};
如何筛选标签?我不明白。其他过滤器是正确的,但标签过滤器是糟糕的,因为承诺
这就是您想要做的吗?
let result = [{ labels: Promise.resolve([{name: "Name1"}, {name: "Name2"}])},{labels: Promise.resolve([{name: "Name3"}, {name: "Name4"}])},];
const filterLabel = "Name2";
const filter = async () => {
let items = [];
for(let r=0; r< result.length; r++) {
const labels = await result[r].labels;
for(let l=0; l< labels.length; l++) {
const label = labels[l];
if(label.name === filterLabel) {
items.push(result[r]);
break;
};
}
}
console.log(items, items.length);
}
filter();