删除记录时接收消息

时间:2019-11-12 05:45:53

标签: typescript nestjs typeorm

这是我从表中删除记录的功能。

async deleteTodo(id: number) {
    this.todosRepository.delete(id);
}

在删除时如何正确接收消息?

例如,当用户发送不存在的记录的标识符时,我希望收到消息“找不到记录”,或者删除时发生错误。

1 个答案:

答案 0 :(得分:1)

.delete does not check if the entity exist in the database

如果要正确检查记录是否存在,可以select对其进行检查并检查返回的值。

示例:

const model = await this.todosRepository
  .createQueryBuilder("todos")
  .where('todos.id= :todelid', { id: todelid })
  .getOne();

if (model.length == 0) {
  return "No record found.";
} else {
  return "Record found.";
}