删除记录时接收消息

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

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

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

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

zhangc321 回答:删除记录时接收消息

.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.";
}
本文链接:https://www.f2er.com/3119707.html

大家都在问