【问题标题】:Receiving messages when deleting a record删除记录时接收消息
【发布时间】:2019-11-12 14:46:16
【问题描述】:

这是我从表中删除记录的函数。

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

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

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

【问题讨论】:

    标签: typescript nestjs typeorm


    【解决方案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.";
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多