保存功能根据记录的保存或更新返回不同的字段

时间:2019-12-09 11:03:35

标签: mysql typeorm

我使用创建套接字来创建和更新任务。

@SubscribeMessage('createToDoToServer')
public async handleCreateToDo(@MessageBody() toDo: IToDo): Promise<void> {
    const toDoDto: CreateToDoDto = {
        title: toDo.title,
        userId: toDo.userId,
    };
    const newToDo: INewToDo = await this.TodoService.saveToDo(toDoDto);
    this.logger.log('ToDo created');
    this.server.emit('createToDoToClient', newToDo);
}

@SubscribeMessage('updateToDoToSever')
public async handleUpdateToDo(@MessageBody() toDo: IToDo): Promise<void> {
    const toDoDto: UpdateToDoDto = {
        id: toDo.id,
        title: toDo.title,
        userId: toDo.userId,
    };
    const newToDo: INewToDo = await this.TodoService.saveToDo(toDoDto);
    this.logger.log('ToDo updated');
    this.server.emit('updateToDoToClient', newToDo);
}

如果任务不在数据库中,则保存功能将保存该任务;如果任务存在,则将其更新。

public async saveToDo(toDoDto: CreateToDoDto): Promise<ToDo> {
    const toDo: ToDo = this.todosRepository.create(toDoDto);
    return await this.todosRepository.save(toDo);
}

创建任务后,函数会将其返回给我。

{
    date:"Mon Dec 09 2019"
    firstName:"Name 1"
    id:1
    lastName:"Name 2"
    title:"new todo"
}

但是当我更新现有任务时,会得到不同的答案。

{
    firstName:"Name 1"
    id:1
    lastName:"Name 2"
    title:"new todo1"
}

为什么同一个函数返回不同的对象?为什么没有日期属性?

这是任务表的结构。日期是在创建记录时形成的,我不发送。

enter image description here

0 个答案:

没有答案